Segmentation Fault Examples: Common Runtime Errors

Explore practical examples of segmentation faults in programming to understand their causes and solutions.
By Jamie

Understanding Segmentation Faults

A segmentation fault (often referred to as a segfault) is a specific kind of error caused by accessing memory that the program does not have permission to access. It typically occurs when a program tries to read or write outside the memory allocated for it. Segmentation faults are common in languages like C and C++ due to manual memory management. Below are three practical examples of segmentation faults that illustrate different contexts in which they can occur.

Example 1: Dereferencing a Null Pointer

In C/C++, dereferencing a null pointer is a frequent cause of segmentation faults. This occurs when a program attempts to access a memory location through a pointer that has not been initialized (i.e., it points to NULL).

Consider a scenario where a function attempts to access the value of a pointer without first checking if it is valid:

#include <stdio.h>

void printValue(int *ptr) {
    // Attempt to print the value pointed to by ptr
    printf("Value: %d\n", *ptr);
}

int main() {
    int *ptr = NULL; // ptr is initialized to NULL
    printValue(ptr); // Dereferencing a NULL pointer
    return 0;
}

In this example, the function printValue tries to dereference ptr, which is NULL. This results in a segmentation fault because the program is trying to access an invalid memory address.

Notes:

  • Always check if pointers are NULL before dereferencing them to avoid segmentation faults.
  • Use tools like Valgrind to help detect null pointer dereferences.

Example 2: Array Index Out of Bounds

Another common cause of segmentation faults is accessing an array out of its bounds. This typically happens when a program tries to access an index that does not exist within the array’s allocated memory.

For instance, consider the following code:

#include <stdio.h>

int main() {
    int arr[5] = {0, 1, 2, 3, 4};
    // Attempting to access the 6th element of the array
    printf("Value: %d\n", arr[5]); // This will cause a segmentation fault
    return 0;
}

In this case, the program attempts to access arr[5], which is outside the bounds of the array arr, leading to a segmentation fault.

Notes:

  • Always ensure that array indices are within valid ranges.
  • Implement bounds checking in your code to prevent such errors.

Example 3: Stack Overflow Due to Infinite Recursion

A segmentation fault can also occur due to a stack overflow, which often happens when there is uncontrolled recursion in a function. Each function call consumes stack space, and if the recursion depth exceeds the stack limit, a segmentation fault will occur.

Here’s a simple example:

#include <stdio.h>

void recursiveFunction() {
    // Infinite recursion
    recursiveFunction();
}

int main() {
    recursiveFunction(); // This will eventually cause a stack overflow
    return 0;
}

In this example, recursiveFunction calls itself indefinitely without an exit condition, which leads to a segmentation fault as the stack space is exhausted.

Notes:

  • Always define a base case for recursive functions to prevent infinite recursion.
  • Monitor stack usage if your application heavily relies on recursive functions.

By understanding these examples of segmentation fault examples, developers can better anticipate and handle runtime errors in their applications, leading to more robust and reliable code.