running c program in Terminal

Discussion in 'Mac Programming' started by rmumma2, Mar 15, 2008.

  1. rmumma2
    Expand Collapse
    macrumors newbie

    Joined:
    Mar 15, 2008
    #1
    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
     
  2. aaronw1986
    Expand Collapse
    macrumors 68030

    Joined:
    Oct 31, 2006
  3. rmumma2
    Expand Collapse
    thread starter macrumors newbie

    Joined:
    Mar 15, 2008
    #3
    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);
    }
     
  4. Eraserhead
    Expand Collapse
    macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #4
    In future please use [code] and [/code] blocks for code :). Also for clarity can you add { and } to the for loop.
     
  5. toddburch
    Expand Collapse
    macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #5
    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
     
  6. rmumma2
    Expand Collapse
    thread starter macrumors newbie

    Joined:
    Mar 15, 2008
    #6
    Thanks Todd for the reply
    and eraserhead I sure will follow ur intstructions from next time onwards
     
  7. lee1210
    Expand Collapse
    macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #7
    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;
    }
    
     

Share This Page