Segmentation Faults: Stack Overflow Examples

Explore practical examples of segmentation faults caused by stack overflow in programming.
By Jamie

Understanding Segmentation Faults Caused by Stack Overflow

A segmentation fault is a common error in programming that occurs when a program tries to access a memory location that it’s not allowed to. One of the frequent causes of segmentation faults is stack overflow, which happens when the call stack pointer exceeds the stack bound. This often occurs due to excessive recursion or large local variable allocations. Below are three diverse examples illustrating how segmentation faults can occur due to stack overflow.

Example 1: Infinite Recursion

In this example, we have a function that calls itself indefinitely. This leads to a stack overflow as each function call consumes a portion of the stack.

In C:

#include <stdio.h>

void recursiveFunction() {
    printf("Calling recursively...\n");
    recursiveFunction(); // Infinite recursion
}

int main() {
    recursiveFunction();
    return 0;
}

Here, recursiveFunction calls itself without a base case to stop the recursion. As the function calls stack up, the program will eventually exhaust the available stack space, leading to a segmentation fault.

Notes:

  • To prevent this, ensure there is a proper base case in recursive functions.
  • Stack overflow due to recursion can often be detected using tools like Valgrind.

Example 2: Large Local Array Allocation

This example demonstrates how allocating a large local array can lead to a stack overflow and subsequently a segmentation fault.

In C++:

#include <iostream>

void largeArrayFunction() {
    int largeArray[100000]; // Large local array
    std::cout << "Array allocated...\n";
}

int main() {
    largeArrayFunction();
    return 0;
}

In this code, largeArrayFunction allocates an array of 100,000 integers on the stack. If the stack size limit is exceeded, a segmentation fault occurs when the program tries to access memory beyond its bounds.

Notes:

  • Consider using dynamic memory allocation (e.g., new in C++ or malloc in C) for large data structures.
  • Check your system’s stack size limit if you encounter this issue frequently.

Example 3: Excessive Function Calls in a Loop

This example shows how a loop that excessively calls a function can lead to stack overflow due to repeated function call stacking.

In Python:

def callFunction():
    print("Function called")

def loopFunction():
    for _ in range(100000):
        callFunction()  # Excessive function calls

loopFunction()

Here, loopFunction calls callFunction 100,000 times in a loop. Each call to callFunction adds a new layer to the stack. If the stack space is exceeded, it results in a segmentation fault.

Notes:

  • To mitigate this, avoid deep recursion and excessive function calls within loops.
  • Profiling tools can help identify stack usage and potential overflow issues.

These examples of example of segmentation fault caused by stack overflow illustrate common pitfalls in programming. By understanding and addressing these issues, developers can create more robust and error-free applications.