Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Mar 15, 2008, 05:26 PM   #1
rmumma2
macrumors newbie
 
Join Date: Mar 2008
running c program in Terminal

Hello everyone,

I have just installed x code tools and compiled a c program
it compiled successfully , but when i try to run it by using ./a.out it is giving Bus error.

Please help me out here.

Regards
Rahul
rmumma2 is offline   0 Reply With Quote
Old Mar 15, 2008, 05:34 PM   #2
aaronw1986
macrumors 68030
 
Join Date: Oct 2006
Could we maybe see some code?
__________________
2012 rMBP 15" 2.6 Ghz, 16GB RAM, 512 GB SSD
2008 Mac Pro 8 core 2.8Ghz, 8800GT, 14GB RAM, 3TB
iPad 2 Black 64GB Verizon
2007 MBP 15" 2.4Ghz Glossy:4GB RAM
aaronw1986 is offline   0 Reply With Quote
Old Mar 15, 2008, 05:36 PM   #3
rmumma2
Thread Starter
macrumors newbie
 
Join Date: Mar 2008
the code

Code:
#include <stdio.h>

int main()
{
        char *p="Hello world";
        int i;
        for(i=0;i< 10;i++)
                *(p+i)='$';
        printf("%s",p);
return (1);
}

Last edited by Mitthrawnuruodo; Mar 15, 2008 at 05:51 PM. Reason: Added code tags...
rmumma2 is offline   0 Reply With Quote
Old Mar 15, 2008, 05:39 PM   #4
Eraserhead
macrumors G4
 
Eraserhead's Avatar
 
Join Date: Nov 2005
Location: UK
In future please use [code] and [/code] blocks for code . Also for clarity can you add { and } to the for loop.
__________________
Actually it does make sense. Man created god, so if we exist, He exists. - obeygiant
Eraserhead is online now   0 Reply With Quote
Old Mar 15, 2008, 06:05 PM   #5
toddburch
macrumors 6502a
 
Join Date: Dec 2006
Location: Katy, Texas
Send a message via AIM to toddburch Send a message via MSN to toddburch
You've defined a constant of "Hello world!" and set p to point to it. So far, so good.

However, you then try to modify the constant, and your compiler has most likely marked the constant to be loaded into protected storage. Thus, the bus error when you attempt to modify read-only stack storage.

Rework your program to either malloc() storage from the heap and copy your constant into it, or, allocate a char array in the stack and copy your constant into that, then modify it.

Todd
toddburch is offline   0 Reply With Quote
Old Mar 15, 2008, 07:28 PM   #6
rmumma2
Thread Starter
macrumors newbie
 
Join Date: Mar 2008
Thanks Todd for the reply
and eraserhead I sure will follow ur intstructions from next time onwards
rmumma2 is offline   0 Reply With Quote
Old Mar 15, 2008, 07:53 PM   #7
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
Quote:
Originally Posted by toddburch View Post
You've defined a constant of "Hello world!" and set p to point to it. So far, so good.

However, you then try to modify the constant, and your compiler has most likely marked the constant to be loaded into protected storage. Thus, the bus error when you attempt to modify read-only stack storage.

Rework your program to either malloc() storage from the heap and copy your constant into it, or, allocate a char array in the stack and copy your constant into that, then modify it.

Todd
Todd was pretty clear, but I felt like implementing this:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

int main(int argc, char **argv) {
  char *p="Hello World";
  int i,end;
  char *copy_p = NULL;
  printf("Before copy: %s\n",p);
  copy_p=(char *)malloc((size_t)(strlen(p) + 1));
  if(!copy_p) { //Hopefully the system can allocate 12 bytes, but for safety...
    fprintf(stderr,"Could not allocate memory for copy_p, errno: %d\n",errno);
    return 0;
  }
  strcpy(copy_p,p); //Only reason not to use strncpy is we just allocated
  printf("After copy, before replace: %s\n",copy_p);
  end=strlen(copy_p); //What is the length of the string literal changes?
  for(i=0;i<end;i++) { //I just replaced the whole string. Your example would leave $$$$$$$$$$d\0 in the buffer, not sure what the intent was
    copy_p[i] = '$'; //Let the compiler do the pointer arithmatic
  }
  printf("After replace: %s\n",copy_p);
  return 1;
}
lee1210 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
How to find out program serial number through Terminal? kathrynk MacBook Pro 3 Nov 26, 2013 06:50 PM
Any way to block the MAC terminal program markinmiami Mac Basics and Help 15 Feb 25, 2013 02:16 AM
Running my C++ in Terminal... Contraversy Mac Programming 3 Nov 4, 2012 10:12 AM
Converting Terminal Window Program to Desktop jamesadrian Mac OS X 10.7 Lion 0 Sep 24, 2012 12:57 PM
WriteRoom-style Terminal program? darwinian Mac Applications and Mac App Store 0 Jul 7, 2012 01:54 PM

Forum Jump

All times are GMT -5. The time now is 04:33 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC