tags:

views:

827

answers:

4

I need to know the root cause of the segmentation fault and also can anyone tell me how to handle it.

+3  A: 

using an invalid/null pointer? Overrunning the bounds of an array? Kindof hard to be specific without any sample code.

Essentially, you are attempting to access memory that doesn't belong to your program, so the OS kills it.

MichaelM
+1  A: 

Here is an example of SIGSEGV.

root@pierr-desktop:/opt/playGround# cat test.c
int main()
{
     int * p ;
     * p = 0x1234;
     return 0 ;
}
root@pierr-desktop:/opt/playGround# g++ -o test test.c  
root@pierr-desktop:/opt/playGround# ./test 
Segmentation fault

And here is the detail.

How to handle it?

  1. Avoid it as much as possible in the first place.

    Program defensively: use assert(), check for NULL pointer , check for buffer overflow.

    Use static analysis tools to examine your code.

    compile your code with -Werror -Wall.

    Has somebody review your code.

  2. When that actually happened.

    Examine you code carefully.

    Check what you have changed since the last time you code run successfully without crash.

    Hopefully, gdb will give you a call stack so that you know where the crash happened.


EDIT : sorry for a rush. It should be *p = 0x1234; instead of p = 0x1234;

pierr
Why would assigning an invalid value to a pointer and not dereferencing that pointer SIGSEGV?
sharptooth
This program will not compile with any C++ compiler. If you add the necessary cast, it will then not crash, as it doesn't actually have any invalid memory access.
Employed Russian
Strictly speaking, forcing an arbitrary value into a pointer object can cause a C/C++ program to crash right away, without a dereference attempt (read up on "trap representations"), but that's not something most of us are likey to encounter in practice. And, of course, this is not a good example to illustrate SIGSEGV :)
AndreyT
Whenever I get segfaults, I just debug with the classic `printf()` method of slowly honing in on where the problem is. (Actually, `puts()` is possibly the best function for this purpose, since it automatically appends a newline, and thus autoflushes the output. But occasionally I need to print out variable values too.)
Chris Lutz
+5  A: 

Wikipedia has the answer, along with a number of other sources.

A segfault basically means you did something bad with pointers. This is probably a segfault:

char *c = NULL;
...
*c; // dereferencing a NULL pointer

Or this:

char *c = "Hello";
...
c[10] = 'z'; // out of bounds, or in this case, writing into read-only memory

Or maybe this:

char *c = new char[10];
...
delete [] c;
...
c[2] = 'z'; // accessing freed memory

Same basic principle in each case - you're doing something with memory that isn't yours.

Chris Lutz
+4  A: 

There are various causes of segmentation faults, but fundamentally, you are accessing memory incorrectly. This could be caused by dereferencing a null pointer, or by trying to modify readonly memory, or by using a pointer to somewhere that is not mapped into the memory space of your process (that probably means you are trying to use a number as a pointer, or you incremented a pointer too far). On some machines, it is possible for a misaligned access via a pointer to cause the problem too - if you have an odd address and try to read an even number of bytes from it, for example (that can generate SIGBUS, instead).

Jonathan Leffler
The different error signals here are poorly defined - here on OS X, `char *c = NULL; *c;` actually generates a SIGBUS rather than a SIGSEGV.
Chris Lutz
Yes - both SIGBUS and SIGSEGV are somewhat system and/or processor specific. Both mean you are abusing memory. Neither is healthy.
Jonathan Leffler