Examples of Segmentation Faults in Linked Lists
What is a Segmentation Fault?
A segmentation fault occurs when a program attempts to access a memory location that it’s not allowed to. This can lead to crashes and instability in your software. In the context of linked lists, segmentation faults often arise due to improper pointer manipulation.
Example 1: Dereferencing a NULL Pointer
One common cause of segmentation faults in linked lists is trying to dereference a NULL pointer. Here’s a simple example in C:
struct Node {
int data;
struct Node* next;
};
void printList(struct Node* head) {
struct Node* current = head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
}
int main() {
struct Node* head = NULL; // head is NULL
printList(head); // This will cause a segmentation fault
return 0;
}
Explanation: In this example, head is initialized to NULL, and when printList attempts to access current->data, it leads to a segmentation fault because you cannot dereference a NULL pointer.
Example 2: Accessing Freed Memory
Another situation that can lead to a segmentation fault is accessing memory that has already been freed. Consider the following code:
struct Node {
int data;
struct Node* next;
};
void freeList(struct Node* head) {
struct Node* current = head;
struct Node* next;
while (current != NULL) {
next = current->next;
free(current);
current = next;
}
}
int main() {
struct Node* head = malloc(sizeof(struct Node));
head->data = 10;
freeList(head);
printf("%d", head->data); // This will cause a segmentation fault
return 0;
}
Explanation: After calling freeList, the memory allocated for head is freed. Attempting to access head->data afterwards results in a segmentation fault because the memory is no longer valid.
Example 3: Incorrect Pointer Assignment
Incorrectly assigning pointers can also lead to segmentation faults. Here’s an example:
struct Node {
int data;
struct Node* next;
};
void insertAfter(struct Node* prev_node, int new_data) {
if (prev_node == NULL) {
printf("The given previous node cannot be NULL.");
return;
}
struct Node* new_node = malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = prev_node->next; // Potential segmentation fault if prev_node is incorrect
prev_node->next = new_node;
}
int main() {
struct Node* head = NULL;
insertAfter(head, 20); // This will cause a segmentation fault
return 0;
}
Explanation: In this case, head is NULL, and passing it to insertAfter leads to a segmentation fault when trying to access prev_node->next.
Conclusion
Understanding how segmentation faults occur in linked lists is vital for debugging and writing robust code. By being aware of pointer manipulation and memory management, you can avoid these common pitfalls and enhance the stability of your programs.
Related Topics
Segmentation Faults: Stack Overflow Examples
Segmentation Faults in Out of Bounds Array Access
Examples of Segmentation Fault in Dynamic Memory Allocation
Segmentation Fault Examples: Null Pointer Dereference
Segmentation Faults in Multithreading: 3 Examples
Segmentation Faults in Recursive Functions
Explore More Segmentation Faults
Discover more examples and insights in this category.
View All Segmentation Faults