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.
Segmentation fault (core dumped)Segmentation fault: 11segfault at 0 ipEXC_BAD_ACCESS (code=1, address=0x0) 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/findresult 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
- Build with
-g -O0 -fsanitize=address,undefinedand re-run - Run
gdb ./appthenrunandbt - Check every pointer returned by an API before using it
Detailed fix by platform
Linux
ulimit -c unlimitedthengdb ./app coreand typebtvalgrind --track-origins=yes ./apppoints at invalid reads and writes
GCC / Clang
- Compile with
g++ -g -O0 -fsanitize=address,undefined -fno-omit-frame-pointer main.cpp
Code examples
Classic null dereference
#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
- Backtrace — Which line does gdb bt show?
- Sanitizer — What does AddressSanitizer report?
- 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:
Similar errors
- AddressSanitizer: heap-buffer-overflow C/C++: ==ERROR: AddressSanitizer: heap-buffer-overflow on address — READ/WRITE of size N
- AddressSanitizer: heap-use-after-free C/C++: ==ERROR: AddressSanitizer: heap-use-after-free on address — memory used after delete/free
- Assertion failed C/C++: Assertion `x > 0' failed. Aborted (core dumped) — assert() condition was false
Most viewed in C/C++
- terminate called after throwing an instance of C++: terminate called after throwing an instance of 'std::out_of_range' — what(): vector::_M_range_check
- undefined reference to C/C++: undefined reference to `function' — collect2: error: ld returned 1 exit status
- expected ';' before C/C++: error: expected ';' before 'x' — missing semicolon
Other ways to find it
Where this error sits
Origin: Application & API. Errors that are often confused with or caused by this one (click to open):
🧠 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.
Report a correction or suggest an improvement
Last updated 15 Oct 2026