Core Dump (Segmentation fault) in C/C++

Core dump (segmentation fault) is a runtime error that occurs when a program tries to access memory that it isn't allowed to access or tries to access memory in a way that isn't allowed. This results in the operating system terminating the program abnormally to prevent potential system damage.

A segmentation fault typically happens when code tries to write to read-only memory, accesses corrupt memory locations, or violates memory access permissions.

Syntax

/* Common scenarios that cause segmentation faults */
char *ptr = NULL;
*ptr = 'A';                    // Dereferencing NULL pointer

char *str = "literal";
str[0] = 'X';                  // Modifying string literal

int arr[5];
arr[10] = 100;                 // Array bounds violation

Example 1: Modifying a String Literal

String literals are stored in read-only memory. Attempting to modify them causes a segmentation fault −

#include <stdio.h>

int main() {
    char *str;
    str = "TutorialsPoint";  /* String literal in read-only memory */
    printf("Original string: %s\n", str);
    
    /* This will cause segmentation fault */
    *(str + 1) = 'X';        /* Trying to modify read-only memory */
    
    return 0;
}

Example 2: Accessing Array Out of Bounds

Accessing memory beyond the allocated array boundaries leads to undefined behavior and potential segmentation fault −

#include <stdio.h>

int main() {
    int arr[2] = {10, 20};
    
    printf("Valid access: arr[0] = %d\n", arr[0]);
    printf("Valid access: arr[1] = %d\n", arr[1]);
    
    /* This will cause segmentation fault */
    arr[3] = 100;            /* Accessing beyond array bounds */
    
    return 0;
}

Example 3: Using Freed Memory

Accessing memory after it has been freed (use-after-free) is a common cause of segmentation faults −

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

int main() {
    int *ptr = malloc(sizeof(int));
    
    if (ptr == NULL) {
        printf("Memory allocation failed\n");
        return 1;
    }
    
    *ptr = 100;
    printf("Value before free: %d\n", *ptr);
    
    free(ptr);               /* Memory is freed */
    
    /* This will cause segmentation fault */
    *ptr = 200;              /* Using freed memory */
    
    return 0;
}

Output

Segmentation fault (core dumped)

Prevention Methods

  • Always initialize pointers before use
  • Check array bounds before accessing elements
  • Set pointers to NULL after freeing memory
  • Use tools like Valgrind to detect memory errors
  • Avoid modifying string literals − use character arrays instead

Conclusion

Segmentation faults are serious runtime errors that indicate memory access violations. Understanding common causes like dereferencing NULL pointers, array bounds violations, and use-after-free helps in writing safer C programs and debugging memory-related issues effectively.

Updated on: 2026-03-15T12:45:54+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements