What causes a SIGSEGV

Vaibhav picture Vaibhav · Oct 14, 2009 · Viewed 121.5k times · Source

I need to know the root cause of the segmentation fault (SIGSEGV), and how to handle it.

Answer

Chris Lutz picture Chris Lutz · Oct 14, 2009

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.