Common causes
- Segmentation fault
A few causes of a segmentation fault can be summarized as follows:
- attempting to execute a program that does not compile correctly. Note that most compilers will not output a binary given a compile-time error.
- a buffer overflow.
- using uninitialized pointers.
- dereferencing NULL pointers.
- attempting to access memory the program does not own.
- attempting to alter memory the program does not own (storage violation).
- exceeding the allowable stack size (possibly due to runaway recursion or an infinite loop)
Generally, segmentation faults occur because a pointer is NULL, or because it points to random memory (probably never initialized to anything), or because it points to memory that has been freed/deallocated/”deleted”.
e.g.
char *p1 = NULL; // Initialized to null, which is OK,
// (but cannot be dereferenced on many systems).
char *p2; // Not initialized at all.
char *p3 = new char[20]; // Great! it's allocated,
delete [] p3; // but now it isn't anymore.
Now, dereferencing any of these variables could cause a segmentation fault.
Recent Comments