Recursion limit

This is a discussion on Recursion limit within the C++ Programming forums, part of the General Programming Boards category; Hey, I looked at the tutorial about recursion, and I saw that it had a limit, before closing the program. ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Location
    82.192.160.50
    Posts
    17

    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.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Nov 2006
    Location
    82.192.160.50
    Posts
    17
    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.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    Do you know what recursion is?

    Calling the same API 130000 times doesn't immediately mean your application will blow up.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Location
    82.192.160.50
    Posts
    17
    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.

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    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.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template Recursion Pickle
    By SevenThunders in forum C++ Programming
    Replies: 20
    Last Post: 02-05-2009, 09:45 PM
  2. limit on recursion?
    By rocketman50 in forum C++ Programming
    Replies: 7
    Last Post: 04-21-2008, 05:46 PM
  3. Replies: 3
    Last Post: 01-08-2004, 09:43 PM
  4. hi need help with credit limit program
    By vaio256 in forum C++ Programming
    Replies: 4
    Last Post: 04-01-2003, 12:23 AM
  5. a simple recursion question
    By tetra in forum C++ Programming
    Replies: 6
    Last Post: 10-27-2002, 10:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21