2

I'm trying to create stack overflow run-time exception with following program:

void f(int a) {
  cout << a << ", ";
  f(++a);
}

int main () {
  f(0);
  return 0;
}

When I run this program, my computer runs about 261824 call stack then the command terminated run-time error occurred. Now I want to know:

  1. Is this a good example of stack overflow? if yes, why did the command terminated error occur?
  2. How can I try, catch stack overflow exception?
  3. I have lots of free memory; why doesn't my stack use all of my memory?
  4. How can I determine the size of stack correspond to my call stack?
freginold
  • 3,640
  • 3
  • 11
  • 26
Milad Khajavi
  • 2,586
  • 8
  • 36
  • 64

2 Answers2

6

These are all implementation details. Technically a C++ implementation need not have a stack, it just needs automatic storage. There is at least one C implementation that used linked lists in the heap (well, sort of -- from what I understand, it is a strange system) for their automatic storage.

But, typically, the stack is a contiguous region of memory address space that the process reserves only for use to store automatic variables and call frames. It must be reserved prior to anything else happening, because it must be contiguous, and if some chunk of memory was allocated for another purpose, the stack would not be able to grow.

If you wanted to use your entire memory address space for the stack, there would be no room for the heap (aka, the free store). So the stack doesn't use all of memory...

1 MB is a traditional value to set the stack to: few programs really need more with even modest avoidance of putting large amounts of data on the stack. In multithreaded environments, each thread ends up with its own stack: so keeping it small also makes threads cheaper. Modern systems probably set it larger, because they have lots of address space for each process.

On 64 bit systems, it would be relatively easy to use 50 bits of address space for the stack (way more than your computer could currently handle: google data centers deal with petabytes). But the downside to this is you would only blow your stack while debugging after your entire system's virtual memory was grabbed by that one process. The upsides to this are not all that large.

The size of the stack is implementation defined, and not exposed by the C++ standard. See your compiler documentation for how to determine how big it is, and how to change its size.

The C++ standard does not dictate what happens when you blow your stack. In general, when the stack is blown, you are probably going to be in serious trouble: write code so that it doesn't happen, rather than catching it happen after it happens.

Yakk - Adam Nevraumont
  • 235,777
  • 25
  • 285
  • 465
4
  1. Yes this is a stack overflow. The error message you get is platform dependent -- so if you ran it on a different type of machine you might see "stack overflow" instead.
  2. There no good portable way to do so as seen here. This is because you're not getting a thrown exception, you're getting the OS killing your process in whatever manner it desires.
  3. Your stack limit is much smaller than your available memory (usually 8-20MB for stack). On specific OS setups you can reconfigure this value if you really need a larger one (ulimit -s 100000 on Solaris sets it to 100MB). But typically hitting the stack limit means you're doing something wrong. A small stack limit helps the OS with virtual memory allocation schemes and is an early error catcher for stack overflows (image a slower version of your code consuming all available memory over an hour or two... not so fun for anything else running).
  4. This can be OS or compiler dependent and isn't something that's baked into C++ inherently. Try this SO link about various approaches to determining stack size.
Community
  • 1
  • 1
Pyrce
  • 7,448
  • 2
  • 25
  • 44