Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Segmentation Fault

  1. Mar 16, 2004 #1
    Segmentation Fault !!!

    What does it mean?
    I often get that in GCC.
    How to overcome that ?
    plz help!
  2. jcsd
  3. Mar 16, 2004 #2
    What are you trying to compile?
  4. Mar 16, 2004 #3
    Just a simple program to open and read a file.
    Declared 2 char pointers in the beginning.
    When i changed 1 of them to char array i cud bypass that Error.
  5. Mar 16, 2004 #4


    User Avatar
    Staff Emeritus

    Your modifying memory that you shouldn't.

    Sometimes this is called a Bus Error
  6. Mar 16, 2004 #5


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    In particular, it sounds like you're forgetting to allocate memory.

    Recall that the point (haha) of a pointer is that it points to memory. So, unless you have an existing block of memory to which you can tell the pointer to point, the pointer is pointing into nothingness, and a segmentation fault occurs.

    Some solutions are:

    Declare an array.

    Memory does get allocated for arrays, so when you declare one, enough space is allocated to hold the array. So, unless you go out of bounds, you don't have any problem.

    Use malloc (or new in C++) to allocate memory, and assign it to the pointer. Don't forget to free (or delete) it when you're done with it!
  7. Mar 25, 2004 #6
    This error often shows up when you call the wrong number of arguments in a function.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook