M
MakeMineScotch
What's the secret to writing recursive functions that won't crash the
computer?
computer?
Make sure that you do not run out of memory???MakeMineScotch said:What's the secret to writing recursive functions that won't crash the
computer?
Sjouke said:MakeMineScotch wrote:
Make sure that you do not run out of memory???
Watch out for recursion to unlimited depth???
MakeMineScotch said:What's the secret to writing recursive functions that won't crash the
computer?
MakeMineScotch said:What's the secret to writing recursive functions that won't crash the
computer?
Jim said:You have to make sure that the recursion can not be infinite. There has to
be a way to end the recursion. There has to be some condition when the
funtion does not call itself, this is usually when a variable is hit or
such.
A simple recursion function would be:
(note, untested code, may have bugs)
int RecursionTest( const int Depth )
{
if ( Depth >= 10 )
return 0;
else
return 1 + Add( Depth + 1 );
}
So that if Depth is greater than 10, it will not call itself, thus ending
the recursion.
Also, I have no idea what value this recursion function would actually call
if you did
int Val = RecursionTest( 0 );
It would return some number but I just did it as an example, not an actually
useful one.
Stefan Naewe said:There is no recursion. You need to call RecursionTest() from
RecursionTest():
Phlip said:The recursions that are not just clever loops, they are stack algorithms,
with a FIFO situation.
What's the secret to writing recursive functions that won't crash the
computer?
Philip said:ITYM LIFO...
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.