Memory access errors occur when a program tries to access memory incorrectly, causing the operating system to terminate the program. The two most common memory access errors are Segmentation Fault (SIGSEGV) and Bus Error (SIGBUS).
- Segmentation Fault (SIGSEGV) occurs when a program accesses memory that it is not permitted to read or write.
- Bus Error (SIGBUS) occurs when a program tries to access an invalid physical memory address or performs an unaligned memory access.
Segmentation Fault (SIGSEGV)
A Segmentation Fault (SIGSEGV) occurs when a program tries to access memory that it is not allowed to access or performs an invalid read/write operation. It is commonly generated as signal 11 and indicates a Segmentation Violation.
- It occurs when a program accesses memory outside its allocated region or attempts to write to read-only memory.
- Common causes include using uninitialized pointers, dereferencing NULL pointers, accessing memory out of bounds, and using dangling pointers.
Common Causes of Segmentation Fault
Segmentation faults usually occur due to invalid memory access during program execution.
- Dereferencing an uninitialized or
NULLpointer. - Accessing an array outside its valid bounds.
- Accessing memory that has already been freed (dangling pointer).
- Writing to read-only memory.
Bus Error (SIGBUS)
A Bus Error (SIGBUS) occurs when a program tries to access a memory address that the hardware cannot physically access. It is commonly generated as signal 10 and usually results from invalid physical memory access or memory alignment issues.
- It occurs when the CPU cannot access the requested memory address because it is invalid or improperly aligned.
- Bus errors are more closely related to hardware memory access restrictions than segmentation faults.
Common Causes of Bus Error
Bus errors typically occur due to hardware-level memory access issues.
- Accessing an invalid physical memory address.
- Reading or writing data from an improperly aligned memory address.
- Accessing memory-mapped files that are no longer valid.
Differences Between Segmentation Fault (SIGSEGV) and Bus Error (SIGBUS)
| Segmentation Fault (SIGSEGV) | Bus Error (SIGBUS) |
|---|---|
| Occurs when a program accesses memory that it is not allowed to access. | Occurs when a program accesses an invalid physical memory address or performs unaligned memory access. |
| Generated as Signal 11 (SIGSEGV). | Generated as Signal 10 (SIGBUS). |
| Commonly caused by NULL pointers, dangling pointers, uninitialized pointers, or array out-of-bounds access. | Commonly caused by invalid physical addresses or memory alignment issues. |
| Indicates an invalid access to a valid memory region. | Indicates access to an invalid or physically inaccessible memory address. |
| Usually caused by software memory access violations. | Usually caused by hardware or memory alignment violations. |
Preventation of Segmentation Fault and Bus Error
These errors can be minimized by following safe memory management practices.
- Always initialize pointers before using them.
- Check pointers for NULL before dereferencing.
- Avoid accessing memory outside array bounds.
- Free dynamically allocated memory only once and avoid using dangling pointers.
- Ensure proper memory alignment when working with low-level memory operations.
- Use debugging tools such as GDB, Valgrind, and AddressSanitizer (ASan) to detect memory access errors.