That seems like a pretty low limit? |
The stack is not designed to hold huge arrays. That's why you have the heap. The stack is there as a quick allocator and to serve as the call stack.
1. Why is stack memory not released upon exit from memoryFunc2()? |
This is just not true. Everything that's constructed on the stack has automatic storage duration, and are always destructed as soon as they go out of scope.
AFAIK, the system is not able to measure stack utilization. The system will tell you how many private bytes a program has allocated, but the stack is always fully allocated, so the system isn't able to distinguish between a full stack and an empty stack. Of course, this depends completely on the system and the implementation. I'm describing Windows and Linux.
You must be looking at the memory footprint of something else.
"If you have to ask, you can't afford it."
Always assume the stack to be very limited. Finding out the stack limit is not very useful, and you shouldn't be allocating terribly large things in it, anyway.
That thread deals mostly with the heap, not the stack.
3. I want to verify there is no way to catch a stack overflow and do graceful recovery from line 24 as mentioned in this article? |
You can pretty much forget about recovery. Once you've corrupted memory, the best you can do is show a friendly message and then quit. It's consensus that you shouldn't let the program crash, but there isn't really much of a difference.
Your myClass has a memory leak of 10^7-1 bytes at line 16.
EDIT: Okay, after testing it, it seems Windows can tell how tall has the stack grown, but doesn't report when the stack shrinks again. You can rest assured that the stack
is being freed, though.