The short answer
SIGSEGV is the name of a signal, which is a short notification the operating system delivers to a process. On Linux and other POSIX systems, the OS sends SIGSEGV when a program makes an invalid memory access. A “segmentation fault” is the crash that follows when nothing handles that signal. The two names describe the same event from different sides: SIGSEGV is the signal, and the segmentation fault is what you see.
What a signal is
A signal is a message the kernel delivers to a process, usually because something happened outside the program’s normal flow. A few common examples:
- SIGINT, sent when you press Ctrl+C in a terminal
- SIGABRT, raised when a program calls
abort() - SIGSEGV, sent when the program makes an invalid memory access
Each signal has a default action. For SIGSEGV, the default is to end the process and, on many systems, to write a core dump file that records the program’s memory for later inspection.
Signal numbers and exit codes
On most Linux systems, SIGSEGV is signal 11. When a process ends because of a signal, the shell reports an exit status of 128 plus the signal number, so a segfault shows up as status 139. You can check this with echo $? right after the crash.
What it looks like in a terminal
Many shells print Segmentation fault (core dumped) when a program crashes. The “(core dumped)” part appears only when the system writes a core dump. Whether that happens depends on settings such as ulimit -c, which sets the maximum size of a core file. Many Linux distributions send core files to a crash handler instead of a file named core, so the location varies from system to system.
If you have a core file, you can load it in gdb with the program and the core file: gdb ./crash core.
Catching SIGSEGV in C++
The std::signal function in <csignal> lets a program install a handler, which is a function the runtime calls when the signal arrives:
#include <csignal>
#include <cstdlib>
void on_segfault(int signal_number) {
// Keep handlers minimal: most library calls aren't safe here
std::_Exit(128 + signal_number);
}
int main() {
std::signal(SIGSEGV, on_segfault);
int* missing = nullptr;
*missing = 1; // Triggers SIGSEGV, which calls the handler
return 0;
}
Build it without optimization, since an optimizer may treat a write through a known null pointer in unexpected ways:
g++ -g -O0 handler.cpp -o handler
With the handler in place, the program exits with status 139 instead of being killed by the default action.
Why a handler rarely fixes the bug
When SIGSEGV comes from a real invalid access, returning from the handler normally makes the program retry the same instruction, which fails again. That’s why the example ends the process. Jumping to another part of the program with longjmp is possible, but it skips destructors and can leave the program’s state damaged, so it’s rarely worth the risk.
Handlers are most useful for a last-chance log or a stack trace. Keep the code inside them small, and avoid heap allocation and locks, since the program may have been partway through changing its own data when it crashed.
Related signals
- SIGABRT comes from
abort()and from runtime checks that detect serious problems, such as some double-free errors. - SIGBUS is a related error, usually linked to misaligned access or problems with memory-mapped files.
A stack overflow on Linux usually shows up as SIGSEGV too. For the broader picture of the crash, see what is segmentation fault in c.
Finding the faulting line
Running the program under gdb is the fastest route. Start with gdb ./crash, type run, and when the signal arrives gdb prints Program received signal SIGSEGV, Segmentation fault and shows the line. Type bt to see the function calls that led to it.
In short
SIGSEGV is the signal, and a segmentation fault is the usual crash it causes when nothing handles it. A handler can record the crash, but the real fix lies in the code that touched the bad address.