Segmentation Fault Examples: Null Pointer Dereference

Explore practical examples of segmentation faults caused by null pointer dereferences to enhance your debugging skills.
By Jamie

Understanding Segmentation Faults Due to Null Pointer Dereference

Segmentation faults are a common issue in programming that occurs when a program attempts to access an area of memory that it’s not allowed to. One frequent cause of segmentation faults is a null pointer dereference. This happens when a program tries to read or write to a memory location through a pointer that is set to null (or zero). In this article, we will explore three practical examples that illustrate how null pointer dereferences can lead to segmentation faults.

Example 1: Dereferencing a Null Pointer in a Function

Context

In many cases, developers use pointers to manage dynamic memory. However, if a pointer is not properly initialized, it can lead to a null pointer dereference when accessed.

Example

#include <stdio.h>

void printValue(int *ptr) {
    printf("Value: %d\n", *ptr);
}

int main() {
    int *nullPtr = NULL;  // Pointer initialized to NULL
    printValue(nullPtr);  // Dereferencing null pointer
    return 0;
}

Notes

In this example, the function printValue tries to dereference ptr, which is nullPtr. Since nullPtr is null, attempting to access the value results in a segmentation fault. To avoid this issue, always check if a pointer is null before dereferencing.


Example 2: Accessing Members of a Struct Through a Null Pointer

Context

Structures are often used in programming to group related data. If a pointer to a structure is not initialized, accessing its members can lead to segmentation faults.

Example

#include <stdio.h>

struct Person {
    char name[50];
    int age;
};

void displayPerson(struct Person *p) {
    printf("Name: %s, Age: %d\n", p->name, p->age);
}

int main() {
    struct Person *personPtr = NULL; // Pointer to struct initialized to NULL
    displayPerson(personPtr);  // Attempting to access members of a null pointer
    return 0;
}

Notes

Here, displayPerson attempts to access the fields of struct Person through personPtr. Since personPtr is null, this leads to a segmentation fault. To prevent this, always ensure the pointer is initialized with a valid memory address before accessing its members.


Example 3: Null Pointer in Array Indexing

Context

When dealing with arrays, pointers are often used to iterate through elements. If a pointer is null, accessing it can result in a segmentation fault.

Example

#include <stdio.h>

int main() {
    int *arrayPtr = NULL;  // Pointer to an array initialized to NULL
    int index = 0;
    printf("Element at index %d: %d\n", index, arrayPtr[index]); // Dereferencing null pointer
    return 0;
}

Notes

In this case, the program tries to access an element of the array using arrayPtr, which is null. This results in a segmentation fault because you cannot dereference a null pointer. Always ensure that your pointers are pointing to valid memory locations before performing operations like indexing.


By examining these examples of segmentation fault due to null pointer dereference, it becomes clear that proper pointer management and checks are crucial in preventing such runtime errors.