Increase recursion depth?

Pages: 12
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.
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.
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.
Without more information on the actual algorithm, we can't help you. It's usually quite easy to cut away some unnecessary memory usage.
If you're putting too much data on the stack, you can always switch to the heap with new and delete
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
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.
By the way, what's your stopping condition? Your code doesn't really show one explicitly.

Depending on conditionN, this could go on forever.
Depending on how friendly your OS is, you can increase the stack size.
Sorry, forgot to show that. Edited.

And yes, Gaminic, I use memoization
Last edited on
And how exactly are the conditionN things defined? Is it guaranteed that conditionN < n? Else, the branch(n) could still repeat forever.
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.
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.
How do you increase the stack size?
Depends on your IDE.
Thanks for the help; I'll work on this a bit more.
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

Side q: Is there a way to use resource for python? I can find it for C++ but not for Python
Last edited on
¿what is the algorithm supposed to do? You could emulate recursion with a stack
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