为什么堆栈溢出会导致分段错误而不是 Linux 中的堆栈溢出?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6986133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why stack overflow causes segmentation fault instead of stack overflow in Linux?
提问by kingsmasher1
Possible Duplicate:
What is the difference between a segmentation fault and a stack overflow?
可能的重复:
分段错误和堆栈溢出有什么区别?
I was just wondering, why stack overflow results in segmentation fault instead of stack overflow.
我只是想知道,为什么堆栈溢出会导致分段错误而不是堆栈溢出。
Is it because the boundary of stack limit is crossed which causes SIGSEGV? Why we don't encounter stack overflow in Linux, and rather a segmentation fault?
是否因为越过堆栈限制的边界而导致 SIGSEGV?为什么我们在 Linux 中不会遇到堆栈溢出,而是出现分段错误?
int foo()
{
return foo();
}
This small code should cause stack overflow but rather it causes segmentation fault in Linux.
这个小代码应该会导致堆栈溢出,但它会导致 Linux 中的分段错误。
回答by MByD
Stackoverflow is not an error, it is a case, the error thrown from it changes from language to language and from platform to platform.
Stackoverflow 不是错误,而是一种情况,从它抛出的错误因语言而异,因平台而异。
See more about segmentation fault in wiki
EDIT:
编辑:
To make it clearer - in your case, the call stack is overflowed and the program tries to write the next call to an invalid address, causing a segmentation fault.
为了更清楚 - 在您的情况下,调用堆栈溢出并且程序尝试将下一个调用写入无效地址,从而导致分段错误。
回答by ninjalj
A stack overflow can cause several different kinds of hardware errors.
堆栈溢出会导致多种不同类型的硬件错误。
- It may lead to an attempt to access memory for which the program has no appropriate permissions → the kernel will raise a
SIGSEGV
(segmentation violation) signal for the process. - It may lead to an attempt to execute an illegal instruction (e.g: you overwrote the return address to point to an invalid instruction) → the kernel will raise a
SIGILL
(illegal instruction) signal. - Probably SIGBUS on some platforms (e.g: alignment exception).
- 它可能会导致尝试访问程序没有适当权限的内存 → 内核将
SIGSEGV
为该进程发出(分段违规)信号。 - 它可能导致尝试执行非法指令(例如:您覆盖返回地址以指向无效指令)→内核将发出
SIGILL
(非法指令)信号。 - 可能是某些平台上的 SIGBUS(例如:对齐异常)。
All these errors occur afterthe stack overflow. An option is to add stack overflow protections (ProPolice, ...), so as to catch stack overflows before they cause more serious problems.
所有这些错误都发生在堆栈溢出之后。一个选项是添加堆栈溢出保护(ProPolice,...),以便在堆栈溢出导致更严重的问题之前捕获它们。
Edit:
编辑:
You mean a "real stack overflow". Well, this case is covered by SEGV (trying to access memory for which the process has no permissions), so it gets a SEGV, instead of special-casing every single case of the more general SEGV.
您的意思是“真正的堆栈溢出”。嗯,这种情况由 SEGV 覆盖(尝试访问进程没有权限的内存),因此它获得了一个 SEGV,而不是对更通用的 SEGV 的每个单独情况进行特殊处理。