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?

Achraf Almouloudi
  • 756
  • 10
  • 27
Alimon Karim
  • 4,354
  • 10
  • 43
  • 68
  • 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.) – Kijewski Aug 15 '14 at 13:46
  • See also http://stackoverflow.com/questions/12687274/size-of-stack-and-heap-memory? – Adrian Aug 15 '14 at 13:46
  • And this http://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap – Adrian Aug 15 '14 at 13:47
  • This is very general question! You can find thousand of similar questions on it – Kaidul Aug 15 '14 at 13:49

1 Answers1

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

Drew McGowen
  • 11,471
  • 1
  • 31
  • 57