1

Here is the code below:

#‎include ‬<stdio.h> int main() { printf("Stack Overflow"); main(); } 

After compiling and executing this program it will print "Stack Overflow" until its stack overflows. Here, I know what a stack overflow means, that means it will print until memory is full. My question is which memory is it? What is the size of the stack that is overflowing?

4
  • 1
    There's a good chance that this code won't produce a stack overflow. Either the compiler will emit a tail call, or just use a while(1) loop. (In this case both optimizations should yield the same assembly code, though.) Commented Aug 15, 2014 at 13:46
  • See also stackoverflow.com/questions/12687274/…? Commented Aug 15, 2014 at 13:46
  • And this stackoverflow.com/questions/79923/… Commented Aug 15, 2014 at 13:47
  • This is very general question! You can find thousand of similar questions on it Commented Aug 15, 2014 at 13:49

1 Answer 1

10

which memory is it?

It is the stack memory, namely that which the program uses to store information during a function call about where to return to. Each time you call a function, the CPU saves the location of what's currently executing onto the stack, then jumps to the new function. When the function is done, it pops that location off the stack and returns there. This is what makes recursion possible.

What is the size of stack that is overflowing?

The actual size of the stack is completely platform-dependent. Many operating systems tend to limit the size of the stack to a few megabytes, but allow that size to be changed. If the system runs out of virtual memory, this can also limit the stack size. Some (much) older CPUs have a fixed-size stack (the Intel 8008 only has 7 stack entries).

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.