-
Limiting recursive stack
Hello,
I have a recursive function and need to put a limit on the stack it can build up (ie. how many times it can call itself). I have two questions.
A. What is a good limit that will work on most machines? Is ten fine?
B. What is a good way of applying the limit? At first I thought I would just use a global or static variable and increment it as I go. The problem with this is how to initialize it. There is obvious problems with initializing it inside the function and I don't want to have to set it before I call the function each time. Is there any neat ways around this?
Thanks heaps!
-
The best way would be to use a static variable. You should just add a parameter to your function that tells when it is initialized.
Example
Code:
int recursive(int s, int init) {
static int iterations;
if(init == -1)
iterations = 10;
if(iterations--);
recursive(s, 0);
}
-
Thanks mate!
That was simple and quick!