Loops alternate recursive ways
#10
(01-30-2023, 05:36 PM)Dimster Wrote: About that STACK. Am I understanding it correctly that our Do Loops, For Loops and even Select Cases, are on and off the stack once the loops are completed, whereas Recursion keep piling onto the Stack to the point where is could over flow the stack. If that's correct, is there a code that can be included with Recursive code that monitors the condition of the stack?

QB64 doesn't really use stackspace like QB45 did.  Instead, we basically use a resizable array to store where we need to return back to, so as long as your PC has memory left, you're not going to run out of stack space.  Think of the process as this:


GOSUB foo --- this does the basic following:

1) Create an internal label for Return_From_foo directly after this GOSUB.  This gives us a point to return back from.
2) Check an internal array for return labels.  Normally it holds XXX number of entries.  If there's already XXX entries in the array, it resizes it to hold XXX + YYY entries.
3) Increment our current array index by 1.   return_index = return_index +1
4) Store this label in the array.    Return_Array(return_index) = "Return_From_foo".
5) GOTO foo

Now, when we run our code and come to a RETURN statement, it basically does the following:

1) Look at the last array element.   Lookup Return_Array(return_index).
2) GOTO that internal label.
3) reduce return_index by 1.   return_index = return_index -1

This gives us a jump point and a return point, and works on a basic FILO principal.  

As long as you have memory for that internal array to keep growing and increasing its size, your program isn't going to crash like it used to do back in QB45.
Reply


Messages In This Thread
Loops alternate recursive ways - by bplus - 01-28-2023, 07:45 PM
RE: Loops alternate recursive ways - by Dimster - 01-28-2023, 09:09 PM
RE: Loops alternate recursive ways - by bplus - 01-28-2023, 09:28 PM
RE: Loops alternate recursive ways - by Dimster - 01-29-2023, 04:10 PM
RE: Loops alternate recursive ways - by bplus - 01-29-2023, 04:23 PM
RE: Loops alternate recursive ways - by Dimster - 01-29-2023, 04:33 PM
RE: Loops alternate recursive ways - by bplus - 01-29-2023, 05:45 PM
RE: Loops alternate recursive ways - by Dimster - 01-30-2023, 05:36 PM
RE: Loops alternate recursive ways - by bplus - 01-30-2023, 07:11 PM
RE: Loops alternate recursive ways - by SMcNeill - 02-01-2023, 06:10 AM
RE: Loops alternate recursive ways - by Dimster - 02-01-2023, 02:05 PM



Users browsing this thread: 2 Guest(s)