Increase recursion depth?

Pages: 12
Dec 6, 2011 at 2:29pm
I have an algorithm that seems to crash C++ due to the recursion depth. I don't know how to change my algorithm (if I could, I would) and I don't want to post it here because it pertains to an online game, but my question is how to increase the recursion depth in something like devcpp+? Thanks!

Or if someone happens to know an easy answer to this:
I am using recursion because each layer involves a loop. The alternative, in my mind, to using recursion would be to use ungodly levels of nested for loops to get all possible combinations of values.
Dec 6, 2011 at 3:07pm
There is no such thing as 'recursion depth settings'. Most likely you're getting a stack overflow because recursion is rather stack-heavy. Even so, the problem is most likely something of this nature:

1
2
3
4
5
void myRecursion() {
    int myInts[LARGE_NUMBER];
    // code
    myRecursion();
}

Lesson of the day: recursion = memory management.

If that's not the error you're getting, you'll probably need to provide more information.
Dec 6, 2011 at 3:14pm
Yeah, the numbers involved (on the order of 9000 or so, where it basically searches for all possible sub-groupings) are quite big. I can get the program to run by arbitrarily shortening the loop inside the function but then my output is slightly off.
Dec 6, 2011 at 3:18pm
Without more information on the actual algorithm, we can't help you. It's usually quite easy to cut away some unnecessary memory usage.
Dec 6, 2011 at 3:21pm
If you're putting too much data on the stack, you can always switch to the heap with new and delete
Dec 6, 2011 at 3:24pm
I've stripped out the stuff that doesn't matter (each line is different but I'll only point to the actual calls) but it looks something along these lines:

editing later

where n starts out around 9000 or so
Last edited on Dec 6, 2011 at 8:09pm
Dec 6, 2011 at 3:28pm
By any chance do function(conditionN) stay constant for certain cases? If so, you could reduce the recursion calls by saving the results of some of them.
Dec 6, 2011 at 3:32pm
By the way, what's your stopping condition? Your code doesn't really show one explicitly.

Depending on conditionN, this could go on forever.
Dec 6, 2011 at 3:32pm
Depending on how friendly your OS is, you can increase the stack size.
Dec 6, 2011 at 3:32pm
Sorry, forgot to show that. Edited.

And yes, Gaminic, I use memoization
Last edited on Dec 6, 2011 at 3:33pm
Dec 6, 2011 at 3:35pm
And how exactly are the conditionN things defined? Is it guaranteed that conditionN < n? Else, the branch(n) could still repeat forever.
Dec 6, 2011 at 3:39pm
It doesn't repeat forever. It works perfectly fine for smaller initial n's. It just breaks when n is too large.

To be more specific, in the loop, I go from 1 to n. I have two variables, x and y, which equal n. x gets passed into some conditions while y is passed into others, so I'm basically splitting everything up every which way. I also exit the loop early if I find that var > bestvar.
Dec 6, 2011 at 3:43pm
Then you'll probably have to turn to Moschops' suggestion of increasing the stack size. Do mind that this will cause problems if you have to send your code to someone else, since his stack size might have a lower limit.
Dec 6, 2011 at 3:44pm
How do you increase the stack size?
Dec 6, 2011 at 3:53pm
Depends on your IDE.
Dec 6, 2011 at 3:58pm
Thanks for the help; I'll work on this a bit more.
Dec 6, 2011 at 4:25pm
Under many *nix, ulimit -s x where x is the new stack size will do it. unlimited is an accepted stack size :p Use at your own risk.

There is also the setrlimit function tucked away in sys/resource.h

Dec 6, 2011 at 4:31pm
Side q: Is there a way to use resource for python? I can find it for C++ but not for Python
Last edited on Dec 6, 2011 at 4:31pm
Dec 6, 2011 at 6:48pm
¿what is the algorithm supposed to do? You could emulate recursion with a stack
Dec 6, 2011 at 7:02pm
I don't know how to use "stack."

I can't make this iterative, if that's what you mean? It'd require thousands of for loops
Pages: 12