Skip to content
All articles

C Programming

What is a segmentation fault and how do you find it? A step-by-step guide with GDB and AddressSanitizer

"Segmentation fault (core dumped)" doesn't tell you what went wrong, only that something did. Here are the most common causes and how to find them in minutes with GDB and AddressSanitizer.

Fahrettin Baştürk5 min read

Everyone who writes their first serious C or C++ program eventually sees this line in the terminal: Segmentation fault (core dumped). No line number, no explanation. The program just dies. Early on, I remember sprinkling printf calls through my code to find it; it took me a while to learn that this is both slow and often misleading.

What is a segmentation fault, really?

The operating system gives every program its own virtual memory space and knows which regions of it may be accessed, and how. When a program tries to access an address that doesn't belong to it, or accesses it in a way it shouldn't (writing to a read-only region, for example), the CPU raises a fault, and the OS sends the program a SIGSEGV signal and terminates it. So a segfault isn't a C error; it's the operating system saying "you can't go there".

The most common causes

  1. Using a NULL pointer: not checking what malloc returned, or reading an uninitialized pointer.
  2. Going past an array's bounds: accessing past the last element with arr[n]. Small overflows often don't segfault and silently corrupt other data instead; big ones do.
  3. Using freed memory: continuing to use a pointer after free.
  4. Stack overflow: infinite recursion, or declaring a huge array on the stack (int huge[10000000];).
  5. Writing to a string literal: char *s = "hello"; s[0] = 'H'; String literals live in a read-only region.
  6. Wrong format specifier: something like printf("%s", number);, treating an integer as an address.

Step 1: compile with debug info

gcc -g -O0 program.c -o program

-g adds line information and -O0 turns off optimization, so the line the debugger shows is really the line you wrote.

Step 2: a backtrace with GDB

gdb ./program
(gdb) run
Program received signal SIGSEGV, Segmentation fault.
0x0000555555555189 in process (node=0x0) at program.c:14
14	    return node->value * 2;
(gdb) bt
#0  process (node=0x0) at program.c:14
#1  main () at program.c:22
(gdb) print node
$1 = (struct Node *) 0x0

Three commands were enough: run ran the program and showed the line where it crashed; bt (backtrace) showed which calls led there; and print told us the problem is that node is NULL. Now the question isn't "why did it crash?" but "why was it NULL?". That's a much easier question.

Step 3: find the real source with AddressSanitizer

Some bugs don't start where they crash; they start much earlier. An array overflow corrupts another variable, and the program crashes much later, somewhere unrelated. GDB shows you where it crashed; AddressSanitizer shows you where the bug *started*:

gcc -g -fsanitize=address,undefined program.c -o program
./program

At the moment of an overflow or use-after-free, ASan stops the program and reports the line, the memory region and how many bytes out of bounds. My habit: always compile with ASan during development. Bugs usually get caught before they ever turn into a segfault.

Looking later with a core dump

If the program crashed on another machine or after a long run, a core dump helps. On Linux, enable it with ulimit -c unlimited, then open it with gdb ./program core and you can inspect the state at the moment of the crash as if you were there.

On Windows

On Windows the same error shows up as an "Access violation" (0xC0000005). Visual Studio's debugger stops right on the offending line at the moment of the crash, and MSVC also supports AddressSanitizer with /fsanitize=address.

Segfaults are frustrating, but they taught me something important: measure instead of guessing. Looking with the right tool instead of searching blindly with printf turns an hours-long hunt into a few minutes. To better understand how memory is managed, the post on malloc and free is a good next step.

Fahrettin Baştürk

Software Engineer & Consultant

Share
  • C
  • C++
  • Debugging
  • GDB

Working on a similar system or problem?Tell me about your project