What is a Segmentation Fault in C/C++ Program



In C++, segmentation fault is a runtime error that occur when your program attempts to access an area of memory that it is not allowed to access. In other words, segmentation fault occur when your program tries to access memory that is beyond the limits that the operating system allocated for your program. These errors are type of access violation that can lead to crashing of program. Segmentation fault occur often with beginner programmers due to the lack of understanding of system level concepts like pointer. In this article, we will explain all the concepts related to segmentation fault, including it's causes of occurrence and how to prevent it.

Table of Content

Why Does Segmentation Fault Occur?

There are several reasons for a program to run into segmentation fault. In general, a program will run into segmentation fault if it tries to:

  • Read or write to a memory that it does not have permission to access.
  • Dereference a null pointer or uninitalized pointer.
  • Access memory that has already been freed or deleted
  • Exceed the bounds of an array
  • Incorrectly casts pointers and accesses invalid memory areas

Common Causes of Segmentation Fault

In the section below, we have explained each cause of segmentation fault with examples.

Cause 1: Dereferencing Null Pointers

A null pointer is a pointer that doesn't point to any valid memory location in the memory of the system. It is initialized with nullptr, which indicates the value zero to the pointer. If you try to access or modify the value stored at the address in which a nullptr points, the program will crash, because there is no valid memory there. an example is given below.

int* ptr = nullptr;
*ptr = 10; // Causes segmentation fault

Cause 2: Dereferencing Uninitalized Pointers

An uninitalized pointers will contain garbage values or random memory address. If you dereference such a pointer, it might point to a memory location that the program is not allowed to access. An example is shown below.

int* ptr; // Uninitialized pointer
*ptr = 5; // May cause segmentation fault

Cause 3: Accessing Freed Memory

Sometimes you may need to free up or delete memory that you allocated using new keyword or malloc keyword. When you are doing so the pointer still holds the same address but the memory is not valid. If you try to use that pointer again, it can lead to a segmentation fault. These type of pointer are called as dangling pointers.

int* ptr = new int(10);
delete ptr;
*ptr = 20; // Dangling pointer - segmentation fault

Cause 4: Array Index Out of Bound

While traversing an array, if you try to access an array element beyond its defined size, you're accessing memory that may belong to something else or even some other programs. C++ does not perform automatic bounds checking for raw arrays. Writing outside the array can corrupt memory or trigger access to protected regions. So accessing outside the boundary of array will always leads to segmentation fault. Let's see an example.

int arr[5];
arr[10] = 3; // Invalid index - segmentation fault

Cause 5: Stack Overflow

Every program has a limited stack size. If a function keeps calling itself recursively for large amount of times, it may exceed the stack size allocated for the program. This will lead to stack overflow and segmentation fault. An example is shown below.

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

Segmentation Fault Example Scenario

In the example code given below, we are trying to attempt to write into a null pointer. This will lead to dereference the null pointer, which will cause the segmentation fault error.

#include <iostream>
using namespace std;

int main() {
    int* ptr = nullptr;
    *ptr = 100; // Attempt to write to a null pointer
    cout << *ptr;
    return 0;
}

The output of the above code will be:

/usr/bin/timeout: the monitored command dumped core
bash: line 1: 268497 Segmentation fault      /usr/bin/timeout 10s main

How to Prevent Segmentation Faults

Here are some precaution you can take to prevent segmentation fault:

  • Always initialize pointers before use.
  • Check for null pointers before dereferencing.
  • Avoid manual memory management whenever possible, prefer containers like std::vector and std::string.
  • Use bounds-checking when accessing arrays or containers.
  • Use tools like Valgrind, AddressSanitizer, or GDB for debugging memory issues.
Updated on: 2025-05-02T18:27:39+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements