What is the difference between a stack overflow and buffer overflow?

The key difference is knowing the difference between the stack and a buffer.

The stack is the space reserved for the executing program to execute in. When you call a function, it's parameter and return info are placed on the stack.

A buffer is a generic chunck of memory that is used for a single purpose. For example, a string is a buffer. It can be over run by writing more data to the string than was allocated for.


Stack overflow refers specifically to the case when the execution stack grows beyond the memory that is reserved for it. For example, if you call a function which recursively calls itself without termination, you will cause a stack overflow as each function call creates a new stack frame and the stack will eventually consume more memory than is reserved for it.

Buffer overflow refers to any case in which a program writes beyond the end of the memory allocated for any buffer (including on the heap, not just on the stack). For example, if you write past the end of an array allocated from the heap, you've caused a buffer overflow.


Stack overflow: you have put too many things on the stack for the memory allocated to the current thread

Buffer overflow: You have exceeded the size of your currently allocated buffer and have not resized it to fit (or cannot resize it further).