Segmentation fault C/C++ Origin: Application & API Last updated 15 Oct 2026

C/C++: Segmentation fault (core dumped) — SIGSEGV from a null, dangling or out-of-bounds pointer

The program touched memory it is not allowed to access — a null or dangling pointer, an out-of-range array index, or a stack overflow — and the OS killed it with SIGSEGV.

Exact message you may see Segmentation fault (core dumped)Segmentation fault: 11segfault at 0 ipEXC_BAD_ACCESS (code=1, address=0x0)
Seen on: C/C++ Linux GCC Clang

Meaning

A segfault is a symptom, not a cause: the real bug can be far from the crash. Compile with -g -fsanitize=address, run under gdb and use bt to see the exact line. Common reasons are dereferencing nullptr, using memory after free, writing past an array, and returning a pointer to a local variable.

Common causes

  • Dereferencing a null pointer (failed fopen/malloc/find result not checked)
  • Use after free or after the owner went out of scope
  • Array or vector index out of range (v[10] on a 3-element vector)
  • Returning a pointer or reference to a local variable
  • Uninitialised pointer or infinite recursion

⚡ Quick fix

  1. Build with -g -O0 -fsanitize=address,undefined and re-run
  2. Run gdb ./app then run and bt
  3. Check every pointer returned by an API before using it

Detailed fix by platform

Linux

  1. ulimit -c unlimited then gdb ./app core and type bt
  2. valgrind --track-origins=yes ./app points at invalid reads and writes

GCC / Clang

  1. Compile with g++ -g -O0 -fsanitize=address,undefined -fno-omit-frame-pointer main.cpp

Code examples

Classic null dereference

cpp
#include <cstdio>

int main() {
    FILE* f = std::fopen("missing.txt", "r");
    // if (!f) { std::perror("fopen"); return 1; }    // the missing check
    char buf[64];
    std::fgets(buf, sizeof buf, f);                    // Segmentation fault: f == nullptr
}

Always check the result of fopen, malloc, dynamic_cast, map::find and similar calls.

How to diagnose

  1. Backtrace — Which line does gdb bt show?
  2. Sanitizer — What does AddressSanitizer report?
  3. Pointer — Could it be null, freed or out of range?

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

Where this error sits

Origin: Application & API. Errors that are often confused with or caused by this one (click to open):

AddressSanitizer: heap-buffer-overflow — C/C++: ==ERROR: AddressSanitizer: heap-buffer-overflow on address — READ/WRITE of size N (C/C++)AddressSanitizer: hea…AddressSanitizer: heap-use-after-free — C/C++: ==ERROR: AddressSanitizer: heap-use-after-free on address — memory used after delete/free (C/C++)AddressSanitizer: hea…Assertion failed — C/C++: Assertion `x > 0' failed. Aborted (core dumped) — assert() condition was false (C/C++)Assertion failedBus error — C/C++: Bus error (core dumped) — SIGBUS from misaligned access or a bad memory-mapped file (C/C++)Bus errorcontrol reaches end of non-void function — C/C++: warning: control reaches end of non-void function [-Wreturn-type] (C/C++)control reaches end o…double free or corruption — C/C++: free(): double free detected / double free or corruption (out) — Aborted (core dumped) (C/C++)double free or corrup…Floating point exception — C/C++: Floating point exception (core dumped) — SIGFPE, usually integer division by zero (C/C++)Floating point except…implicit declaration of function — C: warning: implicit declaration of function 'foo' [-Wimplicit-function-declaration] (C/C++)implicit declaration …Segmentat…

Explore the map of this error Full error map →

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.

💬 What worked for others

No tips yet — did something else fix this for you?

Share what worked for you

Tips are checked by an editor before they appear. Links are removed.