0

My teacher from data structures mentioned it during our lectures today when studying stack, but didn't give proper explanation to it.

sophros
  • 14,672
  • 11
  • 46
  • 75
Corina
  • 137
  • 1
  • 1
  • 12
  • Possible duplicate of [what is the difference between a stack overflow and buffer overflow](https://stackoverflow.com/questions/1120575/what-is-the-difference-between-a-stack-overflow-and-buffer-overflow) – M.K Mar 26 '19 at 10:53
  • Related: [How can a stack underflow happen in C++?](https://stackoverflow.com/questions/6552141/how-can-a-stack-underflow-happen-in-c) (there are some C++-specific details, but overall it should be understandable and generalisable to other languages). – Bernhard Barker Mar 26 '19 at 17:00

2 Answers2

4

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 Listcontains 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!
M.K
  • 1,464
  • 2
  • 24
  • 46
  • And what is stack underflow? – Corina Mar 26 '19 at 11:03
  • Just added it, because I did not see the edit! Look at the answer now! @Corina If the answer was useful and with effort, remember to upvote it! If it solved your problem, mark it as an answer! ^^ – M.K Mar 26 '19 at 11:04
  • What is the difference between . And ->? – Corina Mar 26 '19 at 11:10
  • In this case, I am using `->` to show what the list contains! I'll edit it to make it more clear! @Corina – M.K Mar 26 '19 at 11:11
2

A buffer overflow is a very generic term describing a situation in which you have a buffer filled up with more elements than it should, leading to undefined behaviour. Imagine that you have an array of 1024 bytes, to read 1kb from the network, and you attempt to place more into that.

A call stack overflow is a situation in which there's too many function calls (usually happens when you have unbounded recursion), where exactly because of the number of them, the computer tends to run out of memory for allocating too many stack frames (imagine every stack frame has a default size, about 2MB iirc in linux by default - if you get a call stack tree thousands of functions deep it's possible you'll run out of memory).

NlightNFotis
  • 9,559
  • 5
  • 43
  • 66