Is there a way to check the available stack size before a recursive call? (FROM#)

For a C # AI program, I use a recursive call to find the best next move (using a 30x30 array to store the current state of the board). For each step that I take, I want to see which of the possible steps that I can take from the new state of the board will be better ... and so on, until I reach the end of the game position (no further steps in this state) or a timer stops the process, and further recursive calls are not made (and the "best" known position is returned). This just explains why I should use recursion (this is not tail recursion), and I cannot use one (global) state of the board, but I must look for all possible states of the board from the current state.

(Sometimes) I get a System.StackOverflowException. Is there a way to check the available stack space before the next recursive call? Then I could simply return the current state as β€œthe best position so far found” and not make the next recursive call. That is, when the available stack gets too small, it should also be considered basic.

Another option, of course, could be to simply put each recursive call into a try..catch block and throw a System.StackOverflowException, using it as a base case?

+8
stack c # stack-overflow artificial-intelligence recursion
source share
4 answers

If you really want to go this route, you can use the EnsureSufficientExecutionstack method.

As others have noted, starting with .NET 2.0 you cannot catch a StackOverflowException , however, from the MSDN documentation you know, the previous method has the following behavior:

Ensures that the remaining stack space is large enough to execute the average .NET Framework.

When the stack is not large enough according to this method, it will throw an InsufficientExecutionStackException exception that you can catch .

+1
source share

In fact, the system will dynamically expand the size of the stack if there is no space in the existing stack. That way, even if you can check the stack size, it doesn't really matter.

http://msdn.microsoft.com/en-us/library/windows/desktop/ms686774 (v = vs .85) .aspx more

The system captures additional pages from the reserved memory of the stack, as necessary, until the stack reaches the reserved size minus one page (which is used as a protective page to prevent), or the system is so small in memory that the operation failed. "

Which says that before recursion, the stack is one size; and if recursion causes a stack overflow, when this event occurs, the stack becomes new.

Since you cannot catch a StackOverflowException , you can use tail recursion instead of terminal recursion. The following link provides some useful information on converting terminal return to tail recovery: http://www.thomaslevesque.com/2011/09/02/tail-recursion-in-c/

+2
source share

Instead of recursion, you can use a queue loop ( Queue<TNode> + while (queue.MoveNext()) ) and limit the size of the queue.

Or you can consider a method to be open calls and restrict recursion this way. (Write records and exits and do not enter recursion if records exist> maxOpenCalls).

+2
source share

Starting with .NET 2, you CANNOT catch a StackOverflowException ...

The only way to determine which part of your stack is already in use is to use unsafe code, which I highly recommend ... it is better to use an explicit piece of Stack<T> .

+1
source share

All Articles