Why segmentation fault occurs in C - programming?

Why segmentation fault occurs in C - programming?

A segmentation fault, also known as a segfault, is a specific kind of error caused by accessing memory that “does not belong to you.” This can happen in a variety of ways, such as:

  • Accessing memory that has already been freed

  • Attempting to read or write memory that the program does not have permission to access

  • Overrunning the bounds of an array

  • Dereferencing a null pointer

Some examples are below:

Here's an example of a segmentation fault caused by accessing previously freed memory:

#include <stdio.h>
#include <stdlib.h>

int main() {
    int *ptr = (int *)malloc(sizeof(int));
    free(ptr);
    *ptr = 5; // segmentation fault occurs here
    return 0;
}

In the example above, we use the malloc function to allocate memory for an integer and assign it to a pointer ptr. The memory is then freed using the free function. We still try to assign a value to the memory location pointed to by ptr, which is not permitted and results in a segmentation fault.

Another example of a segmentation fault is caused by overrunning the bounds of an array:

#include <stdio.h>

int main() {
    int arr[10];
    arr[10] = 5; // segmentation fault occurs here
    return 0;
}

In the example above, we declare a 10-int array and attempt to assign a value to the 11th element, which is out of bounds and results in a segmentation fault.

A segmentation fault caused by dereferencing a null pointer:

#include <stdio.h>

int main() {
    int *ptr = NULL;
    *ptr = 5; // segmentation fault occurs here
    return 0;
}

In this example, we declare a pointer ptr and set it to NULL before attempting to assign a value to the memory location pointed to by ptr, which is not permitted and results in a segmentation fault.

Note:

When a segmentation fault occurs, the operating system terminates the program and prints an error message to the console. On Linux systems, the error message is typical "Segmentation fault (core dumped)". The core dump is a file that contains the state of the program at the time of the crash and can be used for debugging purposes.

To avoid segmentation faults, it is important to always check for NULL pointers before dereferencing them and to make sure that any memory that is allocated is properly freed when it is no longer needed. Additionally, it is important to always check for array bounds and to use appropriate data structures such as linked lists or dynamic arrays when working with large amounts of data.

Conclusion:

In conclusion, Segmentation Fault is a serious issue in C programming language and it occurs when the program tries to access memory that it is not supposed to access. It is caused by several reasons like accessing memory that has already been freed, attempting to read or write memory that the program does not have permission to access, Overrunning the bounds of an array and Dereferencing a null pointer. To avoid these errors, checking for NULL pointers, and array bounds, and using appropriate data structures is very important.