What is a segmentation fault?

2018-12-31 00:53发布

What is a segmentation fault? Is it different in C and C++? How are segmentation faults and dangling pointers related?

12条回答
像晚风撩人
2楼-- · 2018-12-31 01:20

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.

查看更多
琉璃瓶的回忆
3楼-- · 2018-12-31 01:24

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

  1. Used before being properly initialised
  2. Used after the memory they point to has been realloced or deleted.
  3. Used in an indexed array where the index is outside of the array bounds. This is generally only when you're doing pointer math on traditional arrays or c-strings, not STL / Boost based collections (in C++.)
查看更多
流年柔荑漫光年
4楼-- · 2018-12-31 01:25

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.

查看更多
浪荡孟婆
5楼-- · 2018-12-31 01:26

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 or scanf statements

Format control string should have the same number of conversion specifiers (%s, %d etc.) as the printf or scanf has arguments to be printed or read. The same applies for fprintf and fscanf.

Not using & on the arguments to scanf

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 use valgrind 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) operators

You 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 in bash.

Debugging using gdb

You can use the debugger gdb to view the backtrace of the core file dumped by your program. Whenever programs segfault, they usually dump the content of memory at the time of the crash into a core file (core dumped). Compile your program with the -g flag, run in gdb and use bt (backtrace).

查看更多
与君花间醉酒
6楼-- · 2018-12-31 01:27

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.

查看更多
唯独是你
7楼-- · 2018-12-31 01:28

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.

 /* "Array out of bounds" error 
   valid indices for array foo
   are 0, 1, ... 999 */
   int foo[1000];
   for (int i = 0; i <= 1000 ; i++) 
   foo[i] = i;

Here i[1000] not exist, so segfault occurs.

Causes of segmentation fault:

it arise primarily due to errors in use of pointers for virtual memory addressing, particularly illegal access.

De-referencing NULL pointers – this is special-cased by memory management hardware.

Attempting to access a nonexistent memory address (outside process’s address space).

Attempting to access memory the program does not have rights to (such as kernel structures in process context).

Attempting to write read-only memory (such as code segment).
查看更多
登录 后发表回答