Possible Duplicate:
Runtime exception, recursion too deep
I'm getting a problem while developing a c#.net program and I've simplified it to a simple problem, I need to understand why does this code throw a stackoverflow exception if I call the function like this :
CheckFunc(16000);
but works fine if I call it like this
CheckFunc(1000);
here is the function :
private void CheckFunc(Int32 i)
{
if (i == 0)
MessageBox.Show("good");
else
CheckFunc(i - 1);
}
tried to make the code as simple as it can get...
I understand that there is a stack that gets overflowed but which stack ? How can I fix this ?
Thanks.
The problem is indeed stack overflow.
The stack is a special memory region, where there are a few things stored:
The problem is that this memory region is limited. Recursive calls will add a significant amount of data on this stack, quickly filling it.
There are several ways:
If this is not enough, the only solution is to find an iterative solution.