What is a segmentation fault? Is it different in C and C++? How are segmentation faults and dangling pointers related?
相关问题
- Sorting 3 numbers without branching [closed]
- Multiple sockets for clients to connect to
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
A segmentation fault is caused by a request for a page that the process does not have listed in its descriptor table, or an invalid request for a page that it does have listed (e.g. a write request on a read-only page).
A dangling pointer is a pointer that may or may not point to a valid page, but does point to an "unexpected" segment of memory.
To be honest, as other posters have mentioned, Wikipedia has a very good article on this so have a look there. This type of error is very common and often called other things such as Access Violation or General Protection Fault.
They are no different in C, C++ or any other language that allows pointers. These kinds of errors are usually caused by pointers that are
It would be worth noting that segmentation fault isn't caused by directly accessing another process memory (this is what I'm hearing sometimes), as it is simply not possible. With virtual memory every process has its own virtual address space and there is no way to access another one using any value of pointer. Exception to this can be shared libraries which are same physical address space mapped to (possibly) different virtual addresses and kernel memory which is even mapped in the same way in every process (to avoid TLB flushing on syscall, I think). And things like shmat ;) - these are what I count as 'indirect' access. One can, however, check that they are usually located long way from process code and we are usually able to access them (this is why they are there, nevertheless accessing them in a improper way will produce segmentation fault).
Still, segmentation fault can occur in case of accessing our own (process) memory in improper way (for instance trying to write to non-writable space). But the most common reason for it is the access to the part of the virtual address space that is not mapped to physical one at all.
And all of this with respect to virtual memory systems.
While Zoul's answer explains what a segmentation fault is, I have found that these kind of bugs can be particular hard to catch, especially if you are new to low-level languages like C++ or C. Here are some of the common ways to get a segmentation fault in your program:
Improper format control string in
printf
orscanf
statementsFormat control string should have the same number of conversion specifiers (
%s
,%d
etc.) as theprintf
orscanf
has arguments to be printed or read. The same applies forfprintf
andfscanf
.Not using
&
on the arguments toscanf
Function
scanf
takes as arguments the format control string and the addresses of variables in which it will place the data that it reads in. The&
(address of) operator is used to supply the address of a variable.Out-of-bounds array references
Make sure that you have not violated the bounds of any array you are using; i.e., you have not subscripted the array with a value less than the index of its lowest element or greater than the index of its highest element.
Valgrind
can come in handy to detect such references - you can usevalgrind
with the--tool=exp-sgcheck
flag.Accessing uninitialized pointers
A pointer variable must be assigned a valid address before being accessed. Make sure that you have initialized all pointers to point to a valid area of memory.
Incorrect use of the
&
(address of) and*
(dereferencing) operatorsYou would need to be careful when using these, especially while passing parameters by reference/using pointers.
Shell Limits
Sometimes segmentation faults are not caused by bugs in the program but are caused instead by system memory limits being set too low. Usually it is the limit on stack size that causes this kind of problem (stack overflows). To check memory limits, use the
ulimit
command inbash
.Debugging using
gdb
You can use the debugger
gdb
to view the backtrace of thecore
file dumped by your program. Whenever programs segfault, they usually dump the content of memory at the time of the crash into acore
file (core dumped
). Compile your program with the-g
flag, run ingdb
and usebt
(backtrace).In simple words: segmentation fault is the operating system sending a signal to the program saying that it has detected an illegal memory access and is prematurely terminating the program to prevent memory from being corrupted.
A segmentation fault or access violation occurs when a program attempts to access a memory location that is not exist, or attempts to access a memory location in a way that is not allowed.
Here i[1000] not exist, so segfault occurs.
Causes of segmentation fault: