分段错误和堆栈溢出有什么区别?
例如,当我们调用递归函数时,连续的调用将存储在堆栈中。但是,如果无限继续下去,则会出现错误,该错误是“分段错误”(如 GCC 上所示)。
难道不应该是“堆栈溢出”吗?那么两者之间的基本区别是什么呢?
顺便说一句,解释比维基百科链接更有帮助(已经浏览过,但没有回答特定的查询)。
For example when we call say, a recursive function, the successive calls are stored in the stack. However, due to an error if it goes on infinitely the error is 'Segmentation fault' (as seen on GCC).
Shouldn't it have been 'stack-overflow'? What then is the basic difference between the two?
Btw, an explanation would be more helpful than wikipedia links (gone through that, but no answer to specific query).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
堆栈溢出是原因,分段错误是结果。
至少在x86和ARM上,“堆栈”是为放置局部变量和函数调用的返回地址而保留的一块内存。当堆栈耗尽时,将访问保留区域之外的内存。但应用程序没有向内核请求该内存,因此会生成 SegFault 以进行内存保护。
Stack overflow is [a] cause, segmentation fault is the result.
At least on x86 and ARM, the "stack" is a piece of memory reserved for placing local variables and return addresses of function calls. When the stack is exhausted, the memory outside of the reserved area will be accessed. But the app did not ask the kernel for this memory, thus a SegFault will be generated for memory protection.
现代处理器使用内存管理器来保护进程免受彼此的影响。 x86 内存管理器有许多遗留功能,其中之一就是分段。分段的目的是防止程序以某些方式操纵内存。例如,一个段可能被标记为只读,代码将放在那里,而另一段是读/写的,这就是您的数据所在的位置。
在堆栈溢出期间,您耗尽了分配给某个段的所有空间,然后您的程序开始写入内存管理器不允许的段,然后您会遇到分段错误。
Modern processors use memory managers to protect processes from each other. The x86 memory manager has many legacy features, one of which is segmentation. Segmentation is meant to keep programs from manipulating memory in certain ways. For instance, one segment might be marked read-only and the code would be put there, while another segment is read/write and that's where your data goes.
During a stack overflow, you exhaust all of the space allocated to one of your segments, and then your program starts writing into segments that the memory manager does not permit, and then you get a segmentation fault.
堆栈溢出可以表现为显式堆栈溢出异常(取决于编译器和体系结构)或分段错误,即无效的内存访问。最终,堆栈溢出是堆栈空间耗尽的结果,而堆栈空间耗尽的一个可能结果是读取或写入不应访问的内存。因此,在许多体系结构上,堆栈溢出的结果是内存访问错误。
A stack overflow can manifest as either an explicit stack overflow exception (depending on the compiler and architecture) or as a segmentation fault, i.e., invalid memory access. Ultimately, a stack overflow is the result of running out of stack space, and one possible result of running out of stack space is reading or writing to memory that you shouldn't access. Hence, on many architectures, the result of a stack overflow is a memory access error.
调用堆栈正在溢出,但是溢出的结果是最终与调用相关的值被推入不属于堆栈的内存中,然后 -
SIGSEGV
!The call stack is being overflowed, however the result of the overflowing is that eventually call-related values are pushed into memory that is not part of the stack and then -
SIGSEGV
!