
-g, run the program inside gdb, and type bt to see the exact line.The message is short and tells you nothing about where the problem is. That is what makes it so frustrating. The good news is that three free tools will point at the exact line.
Everything below was run on our own machine before we wrote it down: gcc 13 on Linux. The outputs are real.
What the message means
Your program runs inside memory the operating system gave it. When it reaches outside that memory, the system sends it a signal called SIGSEGV and ends it.
The GNU C Library manual says the signal “is generated when a program tries to read or write outside the memory that is allocated for it, or to write memory that can only be read”. The name, it says, “is an abbreviation for 'segmentation violation'”.
The Linux manual describes SIGSEGV in three words: “Invalid memory reference”. Its default action is “to terminate the process and dump core”.
What “core dumped” adds. A core dump is, in the Linux manual’s words, “a file containing an image of the process’s memory at the time of termination”, which “can be used in a debugger (e.g., gdb) to inspect the state of the program”. It is a snapshot taken at the moment of the crash.
Why you sometimes see it and sometimes don’t. The bug is the same either way. On our machine the core file size limit was 0, so the shell printed only this:
Segmentation faultAfter we ran ulimit -c unlimited in the same shell, the same program printed the longer message, and a file named core appeared in the folder:
Segmentation fault (core dumped)The message comes from your shell, not from your program. In our runs the shell also reported exit status 139. That is 128 plus 11, and 11 is the number of the SIGSEGV signal.
The 7 usual causes
| Cause | Typical line | What happened in our run |
|---|---|---|
| 1. Null pointer | *p = 42; when p is NULL | Segmentation fault |
| 2. Uninitialized pointer | int *p; *p = 5; | No crash. It printed 5, by luck. |
| 3. Index past the end of an array | scores[5] in an array of 5 | No crash. It printed a wrong total that changed on every run. |
| 4. Writing to a string literal | name[0] = 'A'; when name points to "ada" | Segmentation fault |
5. scanf without & | scanf("%d", age); | Segmentation fault |
| 6. Recursion that never stops | A function that calls itself with no end | Segmentation fault |
7. Using memory after free | data[0] after free(data) | No crash. It printed a garbage number. |
Look at rows 2, 3 and 7. Three of the seven bugs did not crash on our machine. They printed wrong answers quietly. That is the dangerous part of memory bugs in C: a missing crash does not mean the code is right. The same program can crash on another machine, or on your course’s grader.
1. A null pointer
A pointer that holds NULL points at nothing. Reading or writing through it is the classic segmentation fault.
int *p = NULL;
*p = 42;int value = 0;
int *p = &value;
*p = 42;The most common version in homework is a linked list. This loop looks fine and crashes on an empty list, because cur is NULL before the first check:
struct node *cur = head;
while (cur->next != NULL) {
total += cur->value;
cur = cur->next;
}for (struct node *cur = head; cur != NULL; cur = cur->next) {
total += cur->value;
}2. A pointer you never set
A pointer you declare but never assign holds whatever was in memory. The GNU C Library manual lists “dereferencing a null or uninitialized pointer” among the common ways to get this signal. In our run it happened to point somewhere writable, so nothing crashed. Give every pointer a value on the line where you declare it, even if that value is NULL.
3. An index past the end of an array
An array of 5 items has positions 0 to 4. This loop reads position 5:
int scores[5] = {90, 85, 70, 60, 55};
int total = 0;
for (int i = 0; i <= 5; i++) {
total += scores[i];
}for (int i = 0; i < 5; i++) {
total += scores[i];
}The correct total is 360. Our broken version printed 33126 on one run and 33127 on the next. C does not check array positions for you.
4. Writing to a string literal
Text in double quotes is usually stored in memory that can only be read. Changing it is the “write memory that can only be read” case from the manual.
char *name = "ada";
name[0] = 'A';char name[] = "ada";
name[0] = 'A';5. scanf without the &
scanf needs to know where to store the number. Without &, it treats the variable’s current value as an address and writes there.
int age;
scanf("%d", age);int age;
scanf("%d", &age);The compiler can catch this one for you. With -Wall, gcc told us: format '%d' expects argument of type 'int *', but argument 2 has type 'int'.
6. Recursion that never stops
Every call uses a little memory on the stack. A function that keeps calling itself runs out of stack, and the program ends with a segmentation fault.
int count(int n) {
return 1 + count(n + 1);
}int count(int n) {
if (n >= 10) return 0;
return 1 + count(n + 1);
}7. Using memory after you freed it
After free(data), the memory is no longer yours. Reading it may return old values, garbage, or crash. Ours printed 1563391587 on one run and 1646889903 on the next. Set the pointer to NULL right after you free it, and a later mistake becomes an honest crash that is easy to find.
How to find the exact line
Use these three tools in this order. Each one works on the program you already have.
- Turn warnings onCompile with
gcc -g -Wall -Wextra. The-gflag keeps line numbers in the program. The warnings caught causes 2, 5, 6 and 7 for us before the program ever ran. - Run it in gdbType
gdb ./program, thenrun, thenbt. You get the function, the file and the line where it died. - Rebuild with the address sanitizerAdd
-fsanitize=addressand run again. It reports bad memory use even when the program does not crash. - Or run it under ValgrindType
valgrind ./program. No rebuild needed. It reports each invalid read and write with its line.
gdb: where did it die?
The GDB manual defines a backtrace as “a summary of how your program got where it is”. Here is ours for the linked list bug:
Program received signal SIGSEGV, Segmentation fault.
0x000055555555517f in sum (head=0x0) at list.c:12
12 while (cur->next != NULL) {
#0 0x000055555555517f in sum (head=0x0) at list.c:12
#1 0x00005555555551b5 in main () at list.c:21Read it from the top. The crash is in sum, in list.c, on line 12. The argument head=0x0 shows that the list was NULL when main called it from line 21. That is the whole diagnosis.
If you have a core file, you can do the same after the crash, with gdb ./list core and then bt.
The address sanitizer: catch bugs that don’t crash
Our off-by-one loop never crashed. Built with gcc -g -fsanitize=address, it stopped at once:
ERROR: AddressSanitizer: stack-buffer-overflow
READ of size 4 at 0x7f756c400034 thread T0
#0 0x558fc36a4476 in main bounds.c:7
This frame has 1 object(s):
[32, 52) 'scores' (line 4) <== Memory access at offset 52 overflows this variableIt names the line (bounds.c:7), the variable (scores) and the kind of mistake. For the use-after-free bug it printed heap-use-after-free, with the line that read the memory and the line that freed it.
Valgrind: no rebuild needed
Invalid write of size 4
at 0x109161: main (null.c:5)
Address 0x0 is not stack'd, malloc'd or (recently) free'd
Process terminating with default action of signal 11 (SIGSEGV)“Invalid write” at null.c:5, to address 0x0. Address 0 is a null pointer.
Segmentation faults in C++, Python and Java
C++ has all 7 causes, plus a few of its own. We ran three. Using an iterator after the vector grew printed garbage without crashing. Returning a reference to a local variable crashed with a segmentation fault. Calling delete twice on the same pointer was stopped with Aborted, which is a different signal. The address sanitizer named all three. See our C++ homework help page, and our post on C++ compilers if you are choosing one.
Python code does not normally cause this error by itself. When a Python program does die this way, the fault is usually inside a library written in C. Python’s faulthandler module exists for this: its documentation says it can “dump Python tracebacks explicitly, on a fault”, and you can turn it on with the -X faulthandler command line option.
Java reports the same mistakes differently. Recursion that never stops gives a StackOverflowError, which the Java documentation describes as “Thrown when a stack overflow occurs because an application recurses too deeply.”
A checklist before you ask for help
Still crashing?
Some memory bugs hide well. The crash shows up far from the line that caused it, or only with the grader’s input.
That is what our debugging service is for. Send your code and the error. A senior engineer finds the cause, fixes it and explains why it broke. On our debug my code page, a segfault or memory bug is typically $60–$140 and usually takes 1–2 days.
Related: C assignment help, operating systems homework help and data structures homework help.
Questions about segmentation faults
What does segmentation fault (core dumped) mean?
It means your program tried to read or write memory it is not allowed to use, and the operating system stopped it. Core dumped means the system also saved a snapshot of the program’s memory to a file, which a debugger can open to show where the program was when it died.
Why does my program get a segmentation fault only sometimes?
Because the bug depends on what happens to be in memory. An uninitialized pointer or an index past the end of an array can land on memory that is harmless on one run and forbidden on the next. A program that crashes only sometimes still has a bug on every run.
Where is the core file?
It may not exist. If the core file size limit in your shell is 0, no file is written and the message is just Segmentation fault. Run ulimit -c unlimited in the same shell and run the program again. Some systems are also set up to hand core dumps to a program instead of writing a file in your folder.
What is exit code 139?
It is what the shell reports when a program is ended by a segmentation fault. In our runs the status was 139, which is 128 plus 11, the number of the SIGSEGV signal.
How do I find which line caused a segmentation fault?
Compile with the -g option, start the program with gdb, type run, and when it crashes type bt. The first line of the backtrace names the function, the file and the line number where the program died.
Can a segmentation fault happen in Python or Java?
Rarely, and not from ordinary code. In Python it usually comes from a library written in C. In Java, the same mistakes show up as exceptions and errors, such as a StackOverflowError for recursion that never ends.
Sources
- The GNU C Library manual: program error signals
- Linux manual: signal(7) and core(5)
- GDB manual: backtraces
- Python: faulthandler
- Java: StackOverflowError
Tool manuals: GCC instrumentation options and the Valgrind quick start. The outputs shown above are from our own runs of those tools.
Upload the prompt and see a predicted budget in under a minute. A senior engineer confirms the final price.


