What you will find below are 13 answers to relevant questions regarding segmentations faults (often shortened to
segfaults) in C programming language. This article aims to provide you with crisp and accurate insights into
the topic of segmentation faults. Here, a wide range of sub-topics is covered, too.
A segmentation fault is a failure condition or a memory access
violation that leads to a crash of a program. A segfault is caused by dereferencing or assigning to a memory
location that was not
allocated to a given program.
2. Is a segmentation fault the same as a
buffer overrun?
No. A segmentation fault is not the same as a buffer overrun. A buffer overrun is one of many causes of
segmentation faults. Which occurs, when a program attempts to read or write into an illegal memory location. The
code snippet below illustrates just that in C programming language.
Example 1: Buffer overrun in C.
#include <stdio.h>
voidmain()
{
charbuffer[1];
printf( "Enter two symbols to get a buffer overrun: " );
scanf( "%s" , buffer);
}
If you enter one symbol or one character, then buffer overrun does not occur. However, entering more
than one symbol leads to a buffer overrun, because only one character is allocated for the variable
buffer. Accessing any other index than the first element, that is buffer[0], is illegal in this context.
3. What is the difference between a
segmentation fault and a bus error?
Segmentation faults are, at times, equated or mixed up with bus errors because they, too, define a class
of memory access errors.
However, bus errors do differ from segmentation faults. Segfaults happen when physical memory that is
not allocated for a program (but that can be allocated) is being accessed, whereas bus errors happen
when memory that cannot be allocated for a program is being accessed.
In other words, bus errors occur when you try to access a memory address that is meaningless to the
system, unlike segfaults that occur with access of meaningful but not permitted memory address.
4. Is a segfault the same as a buffer
overflow?
No. Also known as buffer overrun, buffer overflow is a potential cause of a segmentation fault, but it is not a
segmentation fault in and of itself.
5. Can a segfault occur because of a
'dangling' pointer?
Absolutely. A 'dangling' pointer or a pointer which points to the memory location that cannot be
accessed anymore, is a common cause of segmentation faults. To see this in C
programming language, consider the code snippet below.
Example 2: The 'dangling' pointer in C.
#include <stdio.h>
voidmain ()
{
char*ptr = NULL;
{
charc;
ptr = &c;
}
//accessing ptr here or
below can lead to a segfault
//...
}
Here (example 2) you can notice an instance of a 'dangling' pointer ptr. The ptr variable
starts 'dangling' on line 9, when the variable c goes out of
declaration scope (on line 8). Outside the scope, the access of the memory address stored inside
the ptr is not legal and can lead to a segfault.
6. Does dereferencing of a NULL pointer cause a
segmentation fault?
Yes, it often does. Dereferencing of a NULL pointer is defined
as an undefined behavior in the C standard and leads to
segmentation faults in many implementations.
Any read or write access of dereferenced NULL pointer is not
legal, because NULL does not point to any memory address, hence
it cannot be dereferenced. To put it simply, a value of a memory address that does not exist cannot be
accessed.
7. Does dereferencing or assigning a 'wild'
pointer cause a segfault?
Yes, a 'wild' pointer can potentially cause a segmentation fault. Also known as uninitialized pointer, a
'wild' pointer contains a random address that is not known to the programmer. Therefore, it is risky to
try to access such a memory address because it leads to undefined and undesirable behavior, segfault
included.
Dereferencing or assigning to a 'wild' pointer is a source of trouble and undefined behavior. It often
leads to segmentation faults, too.
8. Does dereferencing or assigning a freed
pointer cause a segfault?
Yes, a freed, deallocated or a 'dangling' pointer can lead to a segfault. Because it is not legal to
access or modify deallocated memory, whether it is initially initialized on the stack
(as you likely saw in the example 2) or on the
heap.
9. What are some ways to trace segmentation
faults?
Segmentation faults can be traced using quite a number of different methods. To name a few, you can use
debuggers inside such integrated development environments as Visual Studio Code or Visual Studio. You
can also use Valgrind or GDP
debugger to accurately locate the exact lines that cause segfaults.
10. How does a stack overflow differ from a
segmentation fault?
Stack overflow is a plausible cause of a segmentation fault. In other words, a stack
overflow can cause a segmentation fault, but it is not a segfault in and of itself. What stack overflow
is, is a failure condition that occurs when a program runs out of stack memory.
With the lines of code below (example 4), you can achieve a stack overflow and, based on the
implementation, perhaps, a segfault, too (not that it usually is desirable).
Example 4: Stack overflow in C.
intmain(void)
{
returnmain();
}
11. When do standard C library functions
cause segmentation faults?
The standard C library functions can cause segmentation faults when a programmer passes invalid pointers
as arguments. Then, some standard C library will attempt to access illegal memory and result in a
segmentation fault.
12. Which standard C library functions can
lead to segfaults?
13. Does a modification of string literal
cause a segfault?
Yes, modifying a string literal in C programming language is undefined behavior and can be a cause of a
segmentation fault. Also known as read-only strings, string literals should not be modified, as it is stated
in the C99 standard (ISO C99 (Section 6.4.5/6)).
Example 4: Illegal modification of a string literal in C.
voidmain()
{
char*str = "This string literal cannot be modified";
*str = 'b'; // illegal, potential segfault
}
Summary
To sum it all up, segmentation faults are crashes that occur because of memory access violations. To avoid segfaults, you should make sure to keep your pointers valid and avoid read or write access of invalid pointers, be it 'wild', 'dangling', freed or NULL. Such pointers should not be passed to string modifying standard C functions, nor should they be dereferenced or accessed. Avoiding stack and buffer overflows helps, to put it mildly, too.
If a segmentation fault does occur in your program, however, feel free to utilize tools mentioned in the 9th answer or apply understanding that you have likely extracted from other answers.
quizzes and articles!
Please fill out the form below to stay up-to-date to our newest interactive
articles with quizzes.