分段错误和堆栈溢出之间有什么区别?

时间:2020-12-29 03:14:52

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).

例如,当我们调用say,一个递归函数时,连续调用存储在堆栈中。但是,如果它无限地发生错误,则错误是“分段错误”(如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).

顺便说一句,解释比*链接更有帮助(经历过这一点,但没有特定查询的答案)。

4 个解决方案

#1


53  

Stack overflow is [a] cause, segmentation fault is the result.

堆栈溢出是[a]原因,分段错误就是结果。


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和ARM上,“堆栈”是一块内存,用于放置局部变量和返回函数调用的地址。当堆栈耗尽时,将访问保留区域外的存储器。但该应用程序没有向内核询问此内存,因此将生成一个用于内存保护的SegFault。

#2


3  

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!

调用堆栈正在溢出,但溢出的结果是最终调用相关的值被推送到不属于堆栈的内存中,然后 - SIGSEGV!

#3


3  

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.

现代处理器使用内存管理器来保护彼此之间的进程。 x86内存管理器有许多遗留功能,其中之一就是分段。分段旨在防止程序以某种方式操纵内存。例如,一个段可能被标记为只读,代码将被放在那里,而另一个段是读/写,这就是你的数据。

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.

在堆栈溢出期间,耗尽分配给其中一个段的所有空间,然后程序开始写入内存管理器不允许的段,然后出现段错误。

#4


2  

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.

堆栈溢出可以表现为显式堆栈溢出异常(取决于编译器和体系结构)或者作为分段错误,即无效的存储器访问。最终,堆栈溢出是堆栈空间耗尽的结果,堆栈空间耗尽的一种可能结果是读取或写入您不应访问的内存。因此,在许多体系结构中,堆栈溢出的结果是内存访问错误。

#1


53  

Stack overflow is [a] cause, segmentation fault is the result.

堆栈溢出是[a]原因,分段错误就是结果。


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和ARM上,“堆栈”是一块内存,用于放置局部变量和返回函数调用的地址。当堆栈耗尽时,将访问保留区域外的存储器。但该应用程序没有向内核询问此内存,因此将生成一个用于内存保护的SegFault。

#2


3  

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!

调用堆栈正在溢出,但溢出的结果是最终调用相关的值被推送到不属于堆栈的内存中,然后 - SIGSEGV!

#3


3  

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.

现代处理器使用内存管理器来保护彼此之间的进程。 x86内存管理器有许多遗留功能,其中之一就是分段。分段旨在防止程序以某种方式操纵内存。例如,一个段可能被标记为只读,代码将被放在那里,而另一个段是读/写,这就是你的数据。

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.

在堆栈溢出期间,耗尽分配给其中一个段的所有空间,然后程序开始写入内存管理器不允许的段,然后出现段错误。

#4


2  

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.

堆栈溢出可以表现为显式堆栈溢出异常(取决于编译器和体系结构)或者作为分段错误,即无效的存储器访问。最终,堆栈溢出是堆栈空间耗尽的结果,堆栈空间耗尽的一种可能结果是读取或写入您不应访问的内存。因此,在许多体系结构中,堆栈溢出的结果是内存访问错误。