Is there a pre-defined limit on how many times a function can call itself before running out of stack space? If so, is there a way of checking available stack space before each call to avoid a program crash?
Recursive calls and stack space
Miscellaneous Forums/General Discussion/Recursive calls and stack space Is there a pre-defined limit on how many times a function can call itself before running out of stack space?
Depends on your OS. If so, is there a way of checking available stack space before each call to avoid a program crash?
No. > Is there a pre-defined limit on how many times a function can call itself before running out of stack space
Recursion sounds great, but every time I've tried to use it on a real world problem it never works due to stack space. So the limit seems to be "if you are trying to solve a real world problem then you will run out of stack space". I wish I were joking but it's the truth.
Instead of recursion it's always been more reliable to create a list, queue, etc... and fill it with an algorithm that simulates recursion. There is a floodfill example in the code archives that does this, I don't remember which one though.
Recursion sounds great, but every time I've tried to use it on a real world problem it never works due to stack space. So the limit seems to be "if you are trying to solve a real world problem then you will run out of stack space". I wish I were joking but it's the truth.
Instead of recursion it's always been more reliable to create a list, queue, etc... and fill it with an algorithm that simulates recursion. There is a floodfill example in the code archives that does this, I don't remember which one though.
What task are you using recursion for? Recursive function are very handy for things like getting a blitz3d model, loading it and finding every child, assigning it properties... etc, in an easy function.
You know, recursion does not needfully mean that you recall the same function tousands of times ... normally the stack space is 8MB and if you are able to exceed that in every case, you should definitely change something fundamentally in your code.
A recursive function is not meant to hold XY pieces of data ... it explores something, collects information it passes to its caller.
A recursive function is not meant to hold XY pieces of data ... it explores something, collects information it passes to its caller.
I am writing a specialised richtextbox using a MaxGUI canvas. I can't use a standard TextArea because I need very specific control over margins and text style which may vary every line (I am writing a Theatrical script editor). I have therefore coded some wordwrap functions, pseudo code:
The beauty of using recursion here is that processing will stop automatically as soon as words can no longer be wrapped. Unfortunately, with a really long paragraph, this could generate an "Out Of Stack Space" error. Since FlameDuck has confirmed there is no way to monitor available stack space, I have decided to modify the Method to wrap once (this is necessary in order to correctly adjust the claret position), then reformat the whole paragraph:
which will work, but will almost certainly include a lot of unnecessary processing on larger paragraphs.
Repeat Process_Keys() Wrap_WordDown(ClaretLine,1) Forever Method Wrap_WordDown(LineNo:Int,UpdateClaret:Int) If Len(WordAtEndOfLine(LineNo))<GapAtEndOfLine(LineNo+1) ' ' Move word to start of next line here ' If UpdateClaret ' Update Claret parms EndIf Wrap_WordDown(LineNo+1,0) EndIf End Method
The beauty of using recursion here is that processing will stop automatically as soon as words can no longer be wrapped. Unfortunately, with a really long paragraph, this could generate an "Out Of Stack Space" error. Since FlameDuck has confirmed there is no way to monitor available stack space, I have decided to modify the Method to wrap once (this is necessary in order to correctly adjust the claret position), then reformat the whole paragraph:
Method Wrap_WordDown(LineNo:Int,UpdateClaret:Int) If Len(WordAtEndOfLine(LineNo))<GapAtEndOfLine(LineNo+1) ' ' Move word to start of next line here ' If UpdateClaret ' Update Claret parms EndIf Wrap_CurrentParagraph() EndIf End Method
which will work, but will almost certainly include a lot of unnecessary processing on larger paragraphs.
I recently implemented a function (in C++) to read a B3D file without recursion. First I converted the code provided in 'Specs and Utils->Sample Blitz code' link but I couldn't load most of my B3D models because there is recursion when reading chunks. Now without recursion I can read all the models with safe (no matter how large the files are)
Usually a First-In-Last-Out queue helps you to override.
If I remember well you can set how large your program stack will be, but you cannot count on this in real problems as mentioned above.
Usually a First-In-Last-Out queue helps you to override.
If I remember well you can set how large your program stack will be, but you cannot count on this in real problems as mentioned above.
I've used it for floodfills, which should be a perfect use of it, but it always hits the stack space limit.
I rewrote the floodfill to use a queue instead of calling it in a function - no problem then.
I rewrote the floodfill to use a queue instead of calling it in a function - no problem then.