Today, a friend told me thatProgramStackoverflowexception
However, if you do not know why, it is just a recursion.CodeIt seems no problem for a few people to read it.
The following is the simplified code (the actual code is much more complicated, but the general idea is as follows)
Static VoidTest (IntI)
{
If(I>20000) // Or other exit conditions
{
Return;
}
Test (I +1);
}
Static VoidMain ()
{
Test (0);
}
What's the problem? Tip: there are no logic problems, and do not look for any syntax errors.
It looks like the logic is: This Code does not have an endless loop, so it exits 20000 times,
However, an stackoverflowexception exception is thrown during actual operation,
The reason is that the default stack size in the CLR is 1 MB,
This program is continuously recursive, and each level of recursive method is pushed into the stack. When the stack size reaches 1 MB, stackoverflowexception throws
Stack Overflow is thrown when I run to 15918 on my machine.
You may think that recursion is performed more than 10000 times. It seems that there should be no problems.
In fact, as long as you are not careful, hundreds of times of recursion will cause exceptions.
This will be demonstrated later...