Segmentation Fault (SIGSEGV) vs Bus Error (SIGBUS)

16 Dec, 2021
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) 
This article is contributed by Prashanth Pangera. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
gulshankumarar231
C-Pointers
system-programming
C Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Substring in C++
Function Pointer in C
Multidimensional Arrays in C / C++
Different Methods to Reverse a String in C++
Left Shift and Right Shift Operators in C/C++
std::string class in C++
Unordered Sets in C++ Standard Template Library
rand() and srand() in C/C++
Enumeration (or enum) in C
What is the purpose of a function prototype?