This example shows uncontrolled recursion. Eventually, the stack spaced allocated to this process will be completely overwritten by instances of bar and ret...
int foo( int bar )
{
int ret = foo( 42 );
return ret;
}
At least on my system, the call stack seems to be 174602, so you'll need to set the argument to recursion.py to be larger than that; and it takes a few minutes to compile and link the program.
Also, I believe you can get stack overflow if you try to allocate more space than maximum thread stack size ( 1MB by default in VS), so something like int a[100000]; should provide the exception.
Here's one that might happen in practice:
This overflows the stack for negative
x
. And, as Frank Krueger mentioned, also for too largex
(but thenint
would overflow first).This example shows uncontrolled recursion. Eventually, the stack spaced allocated to this process will be completely overwritten by instances of bar and ret...
Keep trying to return main until the stack runs out?
The typical case that does not involve infinite recursion is declaring an automatic variable on the stack that is too large. For example:
If you want to generate an explicitly non-recursive program to result in a stack overflow by function calls:
Sample output:
Sample usage:
At least on my system, the call stack seems to be 174602, so you'll need to set the argument to
recursion.py
to be larger than that; and it takes a few minutes to compile and link the program.As per edit :-)
Also, I believe you can get stack overflow if you try to allocate more space than maximum thread stack size ( 1MB by default in VS), so something like
int a[100000];
should provide the exception.