What is the difference between a segmentation fault and a stack overflow?

AruniRC picture AruniRC · Apr 21, 2010 · Viewed 12k times · Source

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

Answer

kennytm picture kennytm · Apr 21, 2010

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.