Skip to content
Segmentation fault (core dumped)

Segmentation fault (core dumped)

DodaTech 3 min read

Segmentation fault (core dumped) means your program tried to access memory it does not have permission to access. The OS memory protection unit killed the process.

What It Means

The CPU triggered a segmentation fault when your program attempted to read from or write to an invalid memory address. The “core dumped” part means the OS saved a core dump file for debugging.

Why It Happens

  • Dereferencing a NULL or uninitialized pointer
  • Writing past the end of an array (buffer overflow)
  • Using a dangling pointer that points to freed memory
  • Recursive function calls causing stack overflow
  • Modifying a string literal (which is read-only)

How to Fix It

Step 1: Check for NULL pointer dereference

// bad.cpp
int* ptr = nullptr;
*ptr = 42; // segmentation fault!
// fixed.cpp
int* ptr = nullptr;
if (ptr != nullptr) {
    *ptr = 42;
} else {
    ptr = new int(42);
}

Step 2: Avoid buffer overflows

// bad.cpp
int arr[5];
for (int i = 0; i <= 5; ++i) { // off-by-one: writes past the end
    arr[i] = i * 10;
}
// fixed.cpp
int arr[5];
for (int i = 0; i < 5; ++i) {
    arr[i] = i * 10;
}

Step 3: Fix dangling pointers

// bad.cpp
int* ptr = new int(42);
delete ptr;
*ptr = 100; // dangling pointer — undefined behavior
// fixed.cpp
int* ptr = new int(42);
delete ptr;
ptr = nullptr; // good practice
// Do not use ptr again without reassigning

Step 4: Debug with GDB

# Compile with debug symbols
g++ -g crash.cpp -o crash
# Run with GDB
gdb ./crash
(gdb) run
(gdb) bt      # backtrace — shows the call stack
(gdb) info locals  # show local variable values
(gdb) list    # show source code around the crash

Step 5: Detect memory errors with Valgrind

g++ -g crash.cpp -o crash
valgrind --leak-check=full ./crash

Valgrind reports invalid reads/writes, use-after-free, and memory leaks with exact line numbers.

Step 6: Use address sanitizer

g++ -g -fsanitize=address crash.cpp -o crash
./crash

The address sanitizer catches buffer overflows, use-after-free, and memory leaks at runtime with detailed diagnostics.

What is a core dump and how do I use it?
A core dump is a file containing the process memory at the time of crash. Load it in GDB with gdb ./program core. Run bt to see the backtrace. Core dumps may be disabled — enable them with ulimit -c unlimited in your shell.
Why does accessing a string literal cause a segfault?
String literals like "hello" are stored in read-only memory. Writing to them causes a segfault. Use char str[] = "hello" to create a modifiable copy on the stack instead of char* str = "hello".
Can infinite recursion cause a segfault?
Yes. Each function call uses stack memory. Infinite recursion consumes all available stack space, triggering a segmentation fault. Use recursion with a proper base case, or convert deep recursion to an iterative solution.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro