Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is a segmentation fault in C/C++?
A segmentation fault (commonly called "segfault") occurs when your program attempts to access an area of memory that it is not allowed to access. In other words, when your program tries to access memory that is beyond the limits that the operating system allocated for your program.
Segmentation faults are one of the most common runtime errors in C programming and cause the program to terminate immediately with an error message.
Common Causes
Seg faults are mostly caused by pointers that are −
- Used without being properly initialized.
- Used after the memory they point to has been reallocated or freed.
- Used to access array elements outside of the array bounds.
- Dereferenced when they contain invalid addresses.
Example 1: Uninitialized Pointer
This example shows what happens when we try to dereference an uninitialized pointer −
#include <stdio.h>
int main() {
int *ptr; /* Uninitialized pointer */
printf("Attempting to access uninitialized pointer...\n");
*ptr = 10; /* This will cause segmentation fault */
return 0;
}
Attempting to access uninitialized pointer... Segmentation fault (core dumped)
Example 2: Array Bounds Violation
Accessing array elements outside the valid index range can cause segmentation faults −
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
int i;
printf("Valid array access:\n");
for(i = 0; i < 5; i++) {
printf("arr[%d] = %d\n", i, arr[i]);
}
printf("\nAttempting invalid array access...\n");
arr[10] = 100; /* Out of bounds - may cause segfault */
return 0;
}
Valid array access: arr[0] = 1 arr[1] = 2 arr[2] = 3 arr[3] = 4 arr[4] = 5 Attempting invalid array access... Segmentation fault (core dumped)
Example 3: Using Freed Memory
Attempting to access memory after it has been freed is another common cause −
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr = malloc(sizeof(int));
if(ptr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
*ptr = 42;
printf("Value before free: %d\n", *ptr);
free(ptr); /* Memory is freed */
printf("Attempting to access freed memory...\n");
*ptr = 100; /* Using freed memory - segfault */
return 0;
}
Value before free: 42 Attempting to access freed memory... Segmentation fault (core dumped)
Prevention Tips
- Always initialize pointers before using them.
- Check array bounds before accessing elements.
- Set pointers to NULL after freeing memory.
- Use tools like Valgrind to detect memory errors.
- Avoid returning addresses of local variables from functions.
Conclusion
Segmentation faults are serious runtime errors that occur due to improper memory access. Understanding their common causes and following good programming practices helps prevent these crashes and makes your C programs more reliable and stable.
