Never really done much recursion in C++ before but i just wrote a little depth first search which crashes the 1000th time it calls itself. Everytime. Stack Overflow is the error message.
Is the stack limit 1000 or something?
I'm using VC++ 6.0 on Win 2K.
Shud I be able to do recursion with more than 1000 levels?
Otherwise i'll have to attempt some non-recursive way which probably won't look as nice......
Any thoughts wud b well welcome.
There is no static "limit" to recursion except memory. Remember that when you call a function, a copy of that function is loaded into RAM until the function completes. The only drawback to recursion is that no function completes until the final operation is done. That means that on the 1000th call, there is 1000 copies of the function in RAM. Depending on what your function does, you may simply be running out of memory.
>Shud I be able to do recursion with more than 1000 levels?
It depends on the implementation.
>Otherwise i'll have to attempt some non-recursive way which probably won't look as nice......
How many recursive calls do you make in the algorithm? There's a chance that you can remove one or more without too much difficulty.
>> Is the stack limit 1000 or something?
No. When your program starts up, the OS has allocated a certain amount of ram for stack use. Calling a function sets up a sub-stack-frame within this area which 'eats-up' this memory until the function returns. The size of the local variables that the function declares plays a direct role in how much stack space each function call will use up. This could be summed up roughly as:
stack_in_use - (number_of_calls * (variables + overhead))
Where 'stack_in_use' is the amount being used by functions which called this one (ie: main()), and 'overhead' is (usually) 4 bytes that get pushed onto the stack to hold the return address that will be jumped to after the function returns.
So when you're designing a recursive function, the main guidelines are:
1) Declare as few local variables as possible within the function.
2) Make sure the maximum # of possible recursions is a reasonable value.
also, when a process is created, in linux, there is a default limit, set for the amount of ram for a process to own. your user probably can not have more than a thousand copies of that function in memory (size wise).