The short answer
A segmentation fault is triggered when the processor or the operating system rejects a memory access. Before a program can read, write or run code at an address, the hardware checks that the address is mapped to memory the program owns and that the operation is allowed. If a check fails, the processor raises a fault, and the OS delivers SIGSEGV to the program, which normally ends it.
The three checks that usually fail
Most segfaults come from one of three kinds of failed check:
- The address is not mapped. No memory is set up at that address. Address zero is the classic case, because mainstream systems leave the lowest addresses unmapped so that null pointers fail reliably.
- The operation is not permitted. The address is mapped, but the region doesn’t allow that kind of access, such as writing to read-only data.
- The stack runs out of space. Operating systems limit the size of the stack and protect its edge with a guard region. When recursion or a very large local object pushes the stack past its limit, the access fails.
Unmapped addresses
int* p = nullptr;
*p = 1; // Address 0 is not mapped, so the access fails
An uninitialized pointer can fail the same way. It holds a leftover value that may not correspond to any mapped memory:
int* q; // The value is indeterminate
*q = 1; // The address could be anywhere, mapped or not
Writes to read-only memory
Some regions are marked read-only. A write to one is rejected even when the address itself is valid:
const char* text = "abc"; // Literal data is read-only on many systems
const_cast<char*>(text)[0] = 'X'; // Permission check fails: undefined behavior
The const_cast only silences the compiler. The operating system still enforces the protection, so the write fails at run time.
Running past the stack’s limit
Recursion without an exit condition, or a very large local object, uses up the stack. When the stack grows into the guard region at its edge, the access is rejected and the program gets a segmentation fault. The coding mistakes that lead here are covered in what can cause a seg fault.
Why the same bug can crash in different ways
Whether a bad access triggers a fault depends on the exact address. A write that lands in a valid, writable region quietly corrupts data and may not crash at all. A write to an unmapped address fails right away. So a segfault tells you the program did something seriously wrong, but a clean run doesn’t prove the code is correct.
Using the fault to find the problem
Debuggers such as gdb show the line that caused the fault and the address being accessed. A very small address, such as 0x0 or a value near zero, often means a null pointer was used. When a member of a struct is read through a null pointer, the address is the member’s offset from zero, so it stays a small number. When you see one, check the pointer on that line first.
In short
A segmentation fault is triggered by a rejected memory access, and the three checks above account for most cases. Each check protects a rule about which memory a program may touch and how. Once you know which check failed, the bug is usually in the code that produced the bad address.