Thread: What can cause a Segmentation fault?

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    23

    What can cause a Segmentation fault?

    What can cause a Segmentation fault?

    Exactly what is a Segmentation fault?

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    Somewhere you have an array, and you're trying to access an element in a position greater than the array size

    I.e.
    Code:
    int MyArray[5] = {1, 2, 3, 4, 5};
    
    int X = MyArray[100];
    That's the most common thing to throw a Segmentation fault.

    Segmentation fault's happen when you try to access invalid memory (either it hasn't been allocated, you're not permitted access to it, or it's just not there).
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    23
    thanks

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    As Epo says, a segmentation fault results from attempting to access memory that the system doesn't allow to be accessed. In practice, each process is allocated memory in segments, and each segment has different attributes (eg read-only data, read-write, executable, etc). An attempt to do something to memory at odds with attributes of the segment it is in (eg attempting to modify read-only data) results in a segmentation fault.

    The causes aren't only related to falling off the end of arrays. Segmentation faults can also result from pointer molestation. The following are examples of code that can cause segmentation faults (although, occasionally, other errors such as bus errors might the end result)

    Dereferencing a NULL pointer;
    Code:
    char *x = 0;
    *x = 42;        /*  dereferencing a NULL pointer */
    Dereferencing a dangling reference;
    Code:
    char *x = malloc(10);
    free(x);       /*  x is now a dangling reference */
    x[5] = 42;
    Falling off the end of an array (another form of the example given by Epo)
    Code:
    char x[10];
    char *y;
    int i;
    for (y = x; y <= x+10; ++y)
    {
        *y = 0;
    }
    
    for (i = 0; i <= 10; ++i)
    {
         x[i] = 0;
    }
    Both of the loops in this last example will yield undefined behaviour (and potentially a segmentation fault) on the last time through the loop. The way to correct it is to change to loop conditions from "y <= x+10" to "y < x+10" and "i <= 10" to "i < 10".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why am I getting segmentation fault on this?
    By arya6000 in forum C++ Programming
    Replies: 6
    Last Post: 10-12-2008, 06:32 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM