Segmentation Faults

What Triggers a Segmentation Fault?

A segmentation fault is triggered when the CPU or OS rejects a memory access, such as an unmapped address or a write to read-only memory. Learn the checks.

On this page
  1. The short answer
  2. The three checks that usually fail
  3. Unmapped addresses
  4. Writes to read-only memory
  5. Running past the stack’s limit
  6. Why the same bug can crash in different ways
  7. Using the fault to find the problem
  8. In short

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:

  1. 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.
  2. 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.
  3. 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.

The complete guide to segmentation faults

What Is a Segmentation Fault in C++?
Read the guide

Keep exploring

Guides from other topics

All topics →

C++ While Loop Syntax

Learn the C++ while loop syntax, how the condition is checked, and how to avoid infinite loops, with step-by-step examples you can compile and run.

While Loops 4 min read

How to Use goto in C++

Learn how to use goto in C++: write a label and jump to it, see forward and backward examples, avoid compile errors, and pick safer alternatives.

goto 5 min read

The inline Keyword in C++

Learn how the inline keyword in C++ works, why inline functions belong in header files, how inline variables work in C++17, and when the compiler ignores it.

inline 3 min read