39

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

Ether
  • 53,118
  • 13
  • 86
  • 159
AruniRC
  • 5,070
  • 7
  • 43
  • 73

4 Answers4

68

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.

SamB
  • 9,039
  • 5
  • 49
  • 56
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
6

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.

ajs410
  • 2,384
  • 2
  • 19
  • 14
  • 1
    Pretty sure Unix has used this term since long before it came anywhere near x86 ... – SamB Dec 01 '13 at 06:12
  • 3
    @SamB I don't see where the answer claims otherwise. "The x86 memory manager has many legacy features, one of which is segmentation" != "The x86 invented segmentation". – JBentley Jan 25 '14 at 15:22
  • 2
    Segmentation used by x86 (the segment registers) is completely different than "Segmentation" of the address space performed by the operating system. Segmentation Fault has nothing to do with segment registers. Also, althoughh part of x86 for a long time, segment registers are still really important to how x86 works in modern operating systems. – SoapBox Jan 25 '14 at 15:41
  • I'm not sure I understand your point, SoapBox. Segmentation is a concept implemented with segment registers in x86. The registers are used to split the address space into segments, such as the Code Segment, Data Segment, Stack Segment, and Extra Segment. A stack overflow happens when your stack "escapes" the Stack Segment. I'm having trouble identifying the inconsistency that you're trying to point out. – ajs410 Feb 05 '14 at 23:47
  • On all modern operating systems, the segment registers CS, DS, SS are set up in a way that makes them effectively ignored (the extra registers ES,FS,GS can be exceptions used for special purposes). Segmentation Faults do not result from overrunning a CPU segment, but from accessing an invalid page. – user253751 May 21 '15 at 02:34
4

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.

MSN
  • 53,214
  • 7
  • 75
  • 105
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!

Nathan Osman
  • 71,149
  • 71
  • 256
  • 361