/errors/c/segmentation-fault-core-dumped
Segmentation fault (core dumped) in a C program
The commands on this page have not been executed by our verification runner yet.updated
- tool
- C
- exit code
- 139
- versions
- GCC, Clang, Linux (glibc)
- os
- linux, macos
Segmentation fault (core dumped) means the program accessed memory it is not allowed to touch, so the kernel sent it SIGSEGV and stopped it. The shell prints the message, and a core file may be written if core dumps are enabled. The usual causes are a NULL or uninitialized pointer, an array index past the end, or a pointer used after free(). The fastest way to find the line is to rebuild with -g -fsanitize=address and run again, or to run the program under gdb and type bt after the crash.
Example
#include <stdio.h>
#include <string.h>
int main(void) {
char *name = NULL; /* never allocated */
strcpy(name, "ada"); /* write through a NULL pointer: SIGSEGV */
printf("%s\n", name);
return 0;
}gcc -o crash crash.c && ./crash
echo $? # 139 = 128 + 11 (SIGSEGV)Find the faulty line
# AddressSanitizer: reports the invalid access, with file and line
gcc -g -fsanitize=address -fno-omit-frame-pointer -o crash crash.c
./crash
# gdb: stop at the crash and print the call stack
gcc -g -O0 -o crash crash.c
gdb -q ./crash -ex run -ex bt
# Valgrind (Linux): reports invalid reads and writes
valgrind ./crashCore dumps
| Task | Command |
|---|---|
| Allow core files in this shell | ulimit -c unlimited |
| Where the kernel writes them | cat /proc/sys/kernel/core_pattern |
| List and open cores (systemd) | coredumpctl list, coredumpctl gdb |
| Open a core file with gdb | gdb ./crash core |
Sources
Related pages
errors · cC error: implicit declaration of function 'foo' [-Wimplicit-function-declaration]C compilers reject a call to a function with no prior declaration; GCC 14 and Clang 16 made it an error. Include the right header or add a prototype first.flags · cgcc -Wall -Wextra -Werror: enable compiler warningsgcc -Wall -Wextra enables the warnings that catch most C bugs: unused variables, format mismatches, signed comparisons. -Werror makes warnings fail the build.compare · cmalloc vs calloc in C: zeroed memory and overflow checksmalloc leaves memory uninitialized; calloc zeroes it and fails safely when count times size overflows. Differences, cost, and when to use each in C programs.