Open In App

Segmentation Fault (SIGSEGV) vs Bus Error (SIGBUS)

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Segmentation fault(SIGSEGV) and Bus error(SIGBUS) are signals generated when serious program error is detected by the operating system and there is no way the program could continue to execute because of these errors.
1) Segmentation Fault (also known as SIGSEGV and is usually signal 11) occur when the program tries to write/read outside the memory allocated for it or when writing memory which can only be read.In other words when the program tries to access the memory to which it doesn’t have access to. SIGSEGV is abbreviation for “Segmentation Violation”. 
Few cases where SIGSEGV signal generated are as follows, 
-> Using uninitialized pointer 
-> De-referencing a NULL pointer 
-> Trying to access memory that the program doesn’t own (eg. trying to access an array element 
out of array bounds). 
-> Trying to access memory which is already de-allocated (trying to use dangling pointers). 
Please refer this article for examples.
2) Bus Error (also known as SIGBUS and is usually signal 10) occur when a process is trying to access memory that the CPU cannot physically address.In other words the memory tried to access by the program is not a valid memory address.It caused due to alignment issues with the CPU (eg. trying to read a long from an address which isn’t a multiple of 4). SIGBUS is abbreviation for “Bus Error”.
SIGBUS signal occurs in below cases, 
-> Program instructs the CPU to read or write a specific physical memory address which is not valid / Requested physical address is unrecognized by the whole computer system. 
-> Unaligned access of memory (For example, if multi-byte accesses must be 16 bit-aligned, addresses (given in bytes) at 0, 2, 4, 6, and so on would be considered aligned and therefore accessible, while addresses 1, 3, 5, and so on would be considered unaligned.)
The main difference between Segmentation Fault and Bus Error is that Segmentation Fault indicates an invalid access to a valid memory, while Bus Error indicates an access to an invalid address. 
Below is an example of Bus Error taken from wikipedia.
 

C




// C program to demonstrate Bus Error
#include <stdlib.h>
 
int main(int argc, char **argv)
{
    
#if defined(__GNUC__)
# if defined(__i386__)
    /* Enable Alignment Checking on x86 */
    __asm__("pushf\norl $0x40000,(%esp)\npopf");
# elif defined(__x86_64__)
     /* Enable Alignment Checking on x86_64 */
    __asm__("pushf\norl $0x40000,(%rsp)\npopf");
# endif
#endif
 
    /* malloc() always provides aligned memory */
    char *cptr = malloc(sizeof(int) + 1);
     
    /* Increment the pointer by one, making it
       misaligned */
    int *iptr = (int *) ++cptr;
 
    /* Dereference it as an int pointer, causing
       an unaligned access */
    *iptr = 42;
 
    /* Following accesses will also result in
       sigbus error.
       short *sptr;
       int    i;
 
       sptr = (short *)&i;
 
       // For all odd value increments, it will
       // result in sigbus.
       sptr = (short *)(((char *)sptr) + 1);
       *sptr = 100;    */
 
    return 0;
}


Output : 

Bad memory access (SIGBUS) 

 



Last Updated : 16 Dec, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads