Question
Bus Error vs Segmentation Fault in C: SIGBUS and SIGSEGV Explained
Question
What does a bus error message mean, and how does it differ from a segmentation fault?
Short Answer
A bus error and a segmentation fault are both abnormal program terminations caused by invalid memory operations. In C and Unix-like systems, they usually correspond to the signals SIGBUS and SIGSEGV, but they describe different categories of memory-access problems. By the end of this page, you will know the usual causes of each error, how platform behavior can vary, and how to investigate crashes with debugging tools.
Concept
A program does not directly control all computer memory. The operating system and CPU enforce rules about which memory addresses a process may access and how those addresses may be used.
When code breaks one of those rules, the CPU raises a hardware exception. On Unix-like systems, the operating system commonly delivers a signal to the process:
- Segmentation fault (
SIGSEGV): the program accessed memory that is not mapped into its address space, or used memory with prohibited permissions. - Bus error (
SIGBUS): the program made an access that the hardware or operating system cannot complete for another memory-related reason, such as an alignment requirement or an invalid region of a memory-mapped file.
The exact boundary is platform-dependent. A mistake that produces SIGBUS on one CPU or operating system may produce SIGSEGV on another. Therefore, treat the message as an important clue, not as a complete diagnosis.
In C, both errors are often symptoms of undefined behavior: the program has performed an operation that the C language does not define safely. The crash may happen at the bad line, later, or not at all in a different build.
Mental Model
Imagine your process has a rented office building.
- A segmentation fault is like trying to enter a room that is not part of your building, or trying to write on a wall marked “read-only.” Security stops you because you do not have permission.
- A bus error is like entering a room you are allowed to use but trying to plug equipment into a connector in an unsupported way, or using a storage room whose floor has disappeared. The location may exist, but the requested access cannot be completed correctly.
Both are serious access failures. The difference is mainly why the machine rejected the memory operation.
Syntax and Examples
In C, these failures are usually not values you check with if. They are signals raised after an invalid access. The goal is to write code that validates pointers, array bounds, object lifetimes, and I/O results before accessing memory.
A common cause of a segmentation fault is dereferencing a null pointer:
#include <stdio.h>
int main(void) {
int *number = NULL;
printf("%d\n", *number); // Invalid: number points nowhere.
return 0;
}
A safer version ensures the pointer refers to a real object before dereferencing it:
#include <stdio.h>
int main(void) {
int number = 42;
int *pointer = &number;
if (pointer != NULL) {
printf("%d\n", *pointer);
}
return 0;
}
pointer contains the address of , so is a valid access.
Step by Step Execution
Consider this program:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int *scores = malloc(3 * sizeof *scores);
if (scores == NULL) {
return 1;
}
scores[0] = 10;
scores[1] = 20;
scores[2] = 30;
free(scores);
printf("%d\n", scores[0]);
return 0;
}
Step by step:
mallocreserves space for threeintvalues and returns its address inscores.- The null check handles the possibility that allocation failed.
- The assignments to indexes
0,1, and2are valid while the allocation is alive.
Real World Use Cases
These crashes frequently appear in practical software:
- Parsing files or network packets: Reading a length field incorrectly can lead to an out-of-bounds array access and a segmentation fault.
- Memory-mapped files: A program can map a file with
mmap, then access a mapped page whose underlying file was truncated. On many Unix-like systems, this can causeSIGBUS. - Embedded systems: Some processors strictly enforce alignment. Reading a multi-byte value through a wrongly aligned pointer can cause a bus error.
- Image, audio, and database processing: Large buffers make index and size-calculation mistakes more likely.
- Native extensions and libraries: C or C++ code called from another runtime can corrupt memory; the visible crash may occur far from the original bug.
- Concurrent programs: One thread may free or modify memory while another thread still uses it, resulting in an invalid access.
Real Codebase Usage
Production C code reduces these failures through explicit checks and clear ownership rules.
Guard clauses for pointers and inputs
int copy_name(char *destination, size_t capacity, const char *source) {
if (destination == NULL || source == NULL || capacity == 0) {
return -1;
}
snprintf(destination, capacity, "%s", source);
return 0;
}
The function rejects invalid arguments before using them.
Bounds validation
int read_score(const int *scores, size_t count, size_t index, int *result) {
if (scores == NULL || result == NULL || index >= count) {
return -1;
}
*result = scores[index];
return 0;
}
Checking index >= count prevents accessing beyond the array.
Common Mistakes
Assuming every invalid pointer causes the same signal
Do not write logic that depends on receiving SIGBUS rather than SIGSEGV. Signal choice can vary by operating system, CPU architecture, memory layout, and the specific failure.
Dereferencing before checking malloc
Broken:
int *items = malloc(count * sizeof *items);
items[0] = 1; // Invalid if malloc returned NULL.
Better:
int *items = malloc(count * sizeof *items);
if (items == NULL && count != 0) {
return 1;
}
Reading outside array bounds
Broken:
int values[3] = {1, 2, 3};
printf("%d\n", values[3]); // Valid indexes are 0, 1, and 2.
Comparisons
| Aspect | Segmentation fault (SIGSEGV) | Bus error (SIGBUS) |
|---|---|---|
| General meaning | Invalid virtual-memory address or access permission violation | Memory access that cannot be completed for hardware or OS-specific reasons |
| Common causes | Null/dangling pointer dereference, out-of-bounds access, writing read-only memory | Misaligned access on strict CPUs, invalid memory-mapped file access |
| Relation to C | Usually the result of undefined behavior | Usually the result of undefined behavior or an invalid mapped-file condition |
| Platform behavior | Common on Unix-like systems | Meaning and frequency vary more across platforms |
| Debugging approach | Check pointer validity, lifetime, bounds, and permissions | Also check alignment and mmap/file size conditions |
SIGSEGV and are signals, not C exceptions. Ordinary C code does not use / to handle them. Prevent the faulty access, then use debuggers and sanitizers to locate its cause.
Cheat Sheet
- Bus error usually means the OS delivered
SIGBUSafter a memory access could not be completed. - Segmentation fault usually means the OS delivered
SIGSEGVafter an unmapped or forbidden memory access. - Signal details are platform-dependent; do not rely on a specific signal for program logic.
- Frequent
SIGSEGVcauses: null pointers, dangling pointers, array overflows, stack overflows, writes to read-only memory. - Frequent
SIGBUScauses: strict alignment violations and invalid access to a memory-mapped file. - After
free(ptr), do not dereferenceptr; optionally set it toNULL. - Before
array[index], ensureindex < element_count. - Check allocation results when allocation may fail.
- Compile for debugging:
cc -Wall -Wextra -Wpedantic -g -fsanitize=address,undefined file.c -o file
- Investigate crashes with
gdb, especiallyrunandbt.
FAQ
Is a bus error the same as a segmentation fault?
No. Both are memory-related crash signals, but they generally indicate different reasons an access failed. The exact distinction varies by platform.
What causes a bus error in C?
Common causes include accessing incorrectly aligned data on systems that enforce alignment and accessing invalid pages of a memory-mapped file. Some systems use SIGBUS for other low-level memory faults as well.
What causes a segmentation fault in C?
Typical causes are dereferencing NULL, accessing an array outside its bounds, using freed memory, and writing to memory without write permission.
Can an array out-of-bounds error cause either signal?
Yes. Out-of-bounds access is undefined behavior. It may cause SIGSEGV, SIGBUS, corrupt data silently, or appear to work temporarily.
Why does my program crash on one computer but not another?
Undefined behavior depends on details such as CPU architecture, compiler choices, optimization level, operating system memory layout, and timing. A lack of crash does not mean the code is correct.
How do I find the line that caused the crash?
Compile with debug symbols (-g), reproduce the crash in gdb, and run bt for a backtrace. AddressSanitizer is often even more direct for common memory bugs.
Can I safely catch SIGSEGV or SIGBUS and keep running?
Usually no. The program may be in an inconsistent state, and continuing can cause further corruption. Find and fix the invalid memory access instead.
Mini Project
Description
Build a small C program that safely reads a score from a dynamically allocated array. The project practices the checks that prevent common segmentation faults: validating allocation, checking array bounds, and avoiding use-after-free.
Goal
Create a program that prints a requested score when the index is valid and reports an error when it is not.
Requirements
- Allocate storage for five integer scores.
- Initialize all five scores.
- Read one requested index from the command line.
- Reject missing, invalid, negative, or out-of-range indexes.
- Free the allocated memory exactly once before exiting.
Keep learning
Related questions
2D Array Loop Order and Cache Performance in C
Learn why swapping nested loops changes 2D array performance in C, using row-major memory layout, cache locality, and practical benchmarks.
Array-to-Pointer Conversion in C and C++ Explained
Learn what array-to-pointer conversion means in C and C++, how array decay works, and how it differs from a pointer to an array.
Building More Fault-Tolerant Embedded C++ Applications for Radiation-Prone ARM Systems
Learn practical C++ and compile-time techniques to reduce soft-error damage in embedded ARM systems exposed to radiation.