-
Recursion limit
Hey,
I looked at the tutorial about recursion, and I saw that it had a limit, before closing the program. Around 130000 when I tested. Now, I was wondering how I would work around that limit? I need to create a dll that has some artificial intelligence, that should run, at the least, 10 times each second. Now, with a limit of 130000, that would mean the AI would at some point, stop working, which isn't a good thing. So that's my dilemma.
-
Do you need to go ever deeper into recursion? May-be it could all be done without recursion (with regular loops)? What do you use the recursion for?
Infinite recursion will always kill the program. Usually algorithms use recursion only so far when the problem is divided into the smallest chunks that can be eventually solved, and then the algorithm works the way back out of the recursion combining all the small subsolutions. Or something like that.
This particular number probably doesn't mean much, because different functions may require different amounts of stack space (and different machines could provide different amounts of available stack space).
-
Loops might be okay too. As long as it doesn't freeze the application calling it.
EDIT: I was thinking, even if it froze, i could just call it in another thread.
-
Do you know what recursion is?
Calling the same API 130000 times doesn't immediately mean your application will blow up.
-
Well, as i said, the use of this, is for an AI to loop infinitely, but without blocking off the application calling it. But now i use a while along with running the function in a separate thread, and it works perfectly :)
And to answer your question, i think i know, but depends on what you're thinking? The recursion i made was the one from this sites tutorials section.
-
Recursion, at a basic level, has to do with a function calling itself. If your AI stuff is in a main loop calling a function from inside that function over and over with no end, that's a bad thing. ;) You should be using an "endless" interative sort of loop for that kind of thing.... as odd as that might sound.
-
The answer to "how many times can I call back to myself" is not a fixed number for all functions. It depends HEAVILY on the functions parameters and local variables, as each time the function calls itself, it obviously passes the parameters and gets a new set of local (non-static) variables.
I don't think your approach of calling your own function again to prevent the system from locking up is a good approach if there's no decent limit for how many times this can happen - "infinite" recursion doesn't work (unless the compiler decides to make tail-recursion into a loop, but that is a special case). For "unfreezing the system", the best approach is to periodically check if there's something else that needs doing, and then depending on what needs to be done, perform the relevant action. What you do at this point depends much on the actual program itself - if you zoom in PhotoShop and do it quickly, the whole picture will not be drawn, because photoshop interrupts the current redraw and does another zoom - this is very good when working on really large pictures and frequently zooming in and out, as drawing a screen-full of 1024 x 1280 pixels from a much larger picture can be time-consuming.
So, the principle is: If in some long-time-consuming-work, check if something needs to be done, perform the operation (possibly with aborting previous operation).
--
Mats