My teacher from data structures mentioned it during our lectures today when studying stack, but didn't give proper explanation to it.
First, stack and buffer are different things as you may know.
The buffer overflow happens when a program (any kind) tries to write beyond the allocated memory that it has. Imagine
int myArray[5];
myArray[9]=3;
Stack overflow is a specific case. When running, for example a recursive function, the stack which had a reservation already, keeps growing to be bigger than the original reservation!
void recurse()
{
int numbers[20000];
recurse();
}
This will never end. Each function call creates a new stack frame and the stack will eventually consume more memory than is reserved for it.
Check this for more detail!
And stack underflow it is similar to the buffer overflow. With this example you'll understand it!
Imagine you have a list, and you are popping elements.
I assume you know what popping is, but in case you still haven't done that in your subject, it basically is taking out elements. Depending on the type of structure, it will take them from one side or another!
Imagine a list called List
contains three numbers: [1,2,3]
. I'll write it like this: List => [1,2,3]
Which means "List contains [1,2,3]".
List => [1,2,3]
List.pop => [2,3] //List.pop now contains (->) [2,3]
List.pop => [3]
List.pop => []
List.pop => ??? Stack underflow!