Segmentation fault in Linux


In this article, we will discuss segmentation fault, reasons of occurrence of segmentation fault, and fixing or avoiding it along with some examples.

What is the segmentation fault?

A segmentation fault or segfault is a common condition that causes programs to crash. It occurs when a program tries to read or write a memory location that either does not exist or the program doesn’t have the right to access it.

On the operating system level, segmentation is a process of dividing available memory into segments. When it encounters an error while reading or writing a segment Unix based systems send a SIGSEGV signal to the program and it gets crashed after finding this signal.

This generally occurs in low-level programming languages such as C /C++ which require a programmer to manually allocate and deallocate the memory.

Examples

There are several examples that show the occurrence of segmentation fault.

Example 1:

The following example shows segmentation fault caused due to writing in read-only memory.

int main(void)
{
    char *str = "Hello World!";
    *str = 'H';
}

When you execute this code it will produce the runtime error on a Unix or Linux system. On debugging this code you will see –

Program received signal SIGSEGV, Segmentation fault.
0x1c0005c2 in main () at ex1.c:6
6               *str = 'H';

This code can be corrected by using an array instead of using a character pointer.

Example 2:

This example shows the segmentation fault caused due to boundary conditions in the array.

// Array out of bound error
int arr[100];
for (int i = 0; i <= 100 ; i++)
arr[i] = i;

Here arr is defined for index from 0 to 99. However, in the last iteration of the for loop, the program tries to access arr[100]. This will result in a segment fault if the memory location lies outside the memory segment where arr resides.

Causes of segmentation fault

Some typical causes of segmentation faults are –

  • Program attempt to access memory that doesn’t exist
  • Trying to access memory the program doesn’t have the right to
  • Attempting to write read-only memory such as code segments
  • Incorrect pointer manipulation
  • Boundary condition errors
  • Invalid assumptions about shared libraries
  • You are attempting to execute a program that does not compile correctly

How to avoid segmentation fault

You can deal with segfaults by fixing the code causing it. It is generally indicative of poor programming especially array boundary-condition errors, incorrect pointer manipulation, or invalid assumptions about shared libraries.

When you encounter segmentation fault it can be caused due to one of the reasons that are given above. You can check your code for the above reasons or you can use a debugger for example GDB (GNU debugger) to view the backtrace of the core file dumped by your program.

In this way, you can find and fix the segmentation fault error.

Conclusion

I hope you understand what is segmentation fault, its causes, and how to avoid or fix it. Now if you have any queries then write us in the comments below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.