this is Segmentation Fault (SIGSEGV) vs the Bus Error (SIGBUS) coding

Segmentation fault(SIGSEGV) and Bus error(SIGBUS) are alerts generated when critical program error is detected by the working system and there’s no method this system may proceed to execute due to these errors.

1) Segmentation Fault (also referred to as SIGSEGV and is normally sign 11) happen when this system tries to jot down/learn exterior the reminiscence allotted for it or when writing reminiscence which may solely be learn.In different words when this system tries to entry the reminiscence to which it doesn’t have entry to. SIGSEGV is abbreviation for “Segmentation Violation”.

Few circumstances the place SIGSEGV sign generated are as follows,
-> Using uninitialized pointer
-> De-referencing a NULL pointer
-> Trying to entry reminiscence that this system doesn’t personal (eg. making an attempt to entry an array component
out of array bounds).
-> Trying to entry reminiscence which is already de-allocated (making an attempt to make use of dangling pointers).
Please refer this text for examples.

Take a step-up from these “Hello World” applications. Learn to implement data buildings like Heap, Stacks, Linked List and plenty of extra! Check out our Data Structures in C course to start out studying right this moment.

2) Bus Error (also referred to as SIGBUS and is normally sign 10) happen when a course of is making an attempt to entry reminiscence that the CPU can not bodily handle.In different words the reminiscence tried to entry by this system isn’t a legitimate reminiscence handle.It prompted as a consequence of alignment points with the CPU (eg. making an attempt to learn a protracted from an handle which isn’t a a number of of 4). SIGBUS is abbrivation for “Bus Error”.

SIGBUS sign happens in beneath circumstances,
-> Program instructs the CPU to learn or write a selected bodily reminiscence handle which isn’t legitimate / Requested bodily handle is unrecognized by the entire pc system.
-> Unaligned entry of reminiscence (For instance, if multi-byte accesses should be 16 bit-aligned, addresses (given in bytes) at 0, 2, 4, 6, and so forth could be thought of aligned and due to this fact accessible, whereas addresses 1, 3, 5, and so forth could be thought of unaligned.)

 

The main distinction between Segmentation Fault and Bus Error is that Segmentation Fault signifies an invalid entry to a legitimate reminiscence, whereas Bus Error signifies an entry to an invalid handle.

Below is an instance of Bus Error taken from wikipedia.

 

#include <stdlib.h>

 

int main(int argc, char **argv) 

{

    

#if defined(__GNUC__)

# if defined(__i386__)

    

    __asm__("pushfnorl $0x40000,(%esp)npopf");

# elif defined(__x86_64__) 

     

    __asm__("pushfnorl $0x40000,(%rsp)npopf");

# endif

#endif

 

    

    char *cptr = malloc(sizeof(int) + 1);

     

    

       

    int *iptr = (int *) ++cptr;

 

    

       

    *iptr = 42;

 

    

       

       

       

 

       

 

       

       

       

       

 

    return 0;

}

Output :

Output :

Bad reminiscence entry (SIGBUS)

This article is contributed by Prashanth Pangera. If you want GeeksforGeeks and want to contribute, you too can write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article showing on the GeeksforGeeks main web page and assist different Geeks.

Please write feedback for those who discover something incorrect, otherwise you wish to share extra details about the subject mentioned above.

 

Add a Comment

Your email address will not be published. Required fields are marked *