What is a bus error? Is it different from a segmentation fault?

There are two kinds of errors, bus error(SIGBUS) and segmentation fault(SIGSEGV) in C or C++ programs. The segmentation fault generates signal 11 (memory not assigned), while the bus error calls signal 10 (access to the invalid memory). Usually, these errors can be faced when the user tries to write his program. 

This post will cover an explanation of the bus error and segmentation fault.

Bus Error

A bus error is known as Signal Bus (SIGBUS). It can be faced when the user tries to access the memory, which is invalid. Simply put, a process is trying to access that is not physically aligned with the CPU. Below are the causing factors of the bus error.

Factors:

  • Invalid Access to the Memory
  • Unaligned Memory Address means (For example, the given Memory address is 16 bytes and the given is 0,1,2,3. Remaining is considered unaligned)

Let’s understand this error using the c program, which is generating the bus error.

Example: Invalid and Unaligned Memory Address 

The bus error output will be printed in this example. This code looks complicated, but you just need to understand the concept. 

Just look at the “*cptr” pointer of character type, which is being allocated with the size of an integer, and in the next line, “*iptr” unaligned it by incrementing the “*cptr” pointer. So if we store any integer in the “*iptr,” this will generate the bus error:

#include <stdlib.h>
int main(int argc, char **argv)
{
   
#if defined(__GNUC__)
# if defined(__i386__)
    __asm__("pushf\norl $0x40000,(%esp)\npopf");
# elif defined(__x86_64__)
    __asm__("pushf\norl $0x40000,(%rsp)\npopf");
# endif
#endif
    char *cptr = malloc(sizeof(int) + 1);
    int *iptr = (int *) ++cptr;
    *iptr = 42;
    return 0;
}

The following is the output of this program:

$ gcc c-program.c -o output

The bus error is displayed.

Segmentation Fault

The segmentation fault can be faced when a program tries to access the memory in which it doesn’t have any. It is also known as SiGSEGV (Segmentation Violation). The following are the factors in which users can face the segmentation fault error.

Factors:

  • Dereferencing the Pointer
  • Using the uninitialized pointer
  • Try to modify the read-only memory
  • Try to access the Memory that is deallocated

Example 1: Modifying the Read-only Memory

In the below example of the C++ program, we created a spring pointer of character type pointing to the “itslinuxfoss”. In the next line, we try to modify it by addressing it to the “n”. So this will cause the error:

#include <iostream>
using namespace std;
int main()
{
  char *string;
  string = "itslinuxfoss";//Read only segment is allocated
  *(string + 1) = 'n'; // trying to modify read-only Memory
  return 0;
}

In the output, the program will be terminated:

$ g++ program.cpp

The error says that you are trying to convert a constant string. 

Example 2: Accessing the Freed Up Location 

In the second example of C++, we are using the de-allocated pointer:

#include <iostream>
using namespace std;
int main()
{
    int* pointer;
    *pointer = 100;
    free(pointer); //Memory Freed Up

    *pointer = 110;//Now this statement will be illegal
        cout<<*pointer;
    return 0;
}
$ g++ program.cpp

Example 3: Accessing Apart From the Array Index

In the following example, we have initialized an array of size five and are trying to access the sixth location of the array index, which is not allocated to the array. So this will terminate the program and give the error: 

#include <iostream>
using namespace std;

int main()
{
  int array[5];
  array[6] = 10;  // Accessing Outside the array index
  return 0;
}

Check the output of the above program:

$ g++ program.cpp

The above image has displayed the core dumped error (segmentation fault). 

Example 4: Printing the Uninitialized Pointer 

In the below-given program of C++, we are using uninitialized pointers that are generating the segmentation fault error:

#include <iostream>
using namespace std;
int main(){

int *pointer;
cout << *pointer;

return 0;
}

The following is the output of the above program:

$ g++ program.cpp

The segmentation fault error is printed on the screen.

Conclusion

The bus error occurs when a program tries to access an invalid address. In contrast, the segmentation fault occurs when invalid access is given to the program apart from a valid memory address. This post has illustrated a detailed explanation of the bus error and segmentation fault with the help of examples.