I'm working on developing a timer that performs some action every 30 ms. I want to do this using only LibreOffice Basic. I tried creating a recursion, but the stack overflowed. I also tried using loops, but they run slower, which is noticeable even to the eye. Then my research supervisor found GoTo
. Using GoTo
, I want to implement a recursion. I've run this code, and the stack didn't overflow, but I'm curious if it could overflow, and if so, when? For example, the nested recursion lives for around 3-3.5k calls (I've verified this).
Sub loopWork
GoTo Label1
Label1:
if stateMainTimer = 2 Then
Exit Sub
End If
GoTo Label2
Label2:
loopWorkVoid()
GoTo label1
End Sub
This structure has supported over 35,000 calls.
I ran recursion without GoTo
, and it holds up to about 3,000-3,500 calls before overflowing. I added a for loop, then called recursion, and that structure lasted around 20,000 calls. With this structure (GoTo
), it has been holding for more than 75,000 calls already, and I'm curious about the limit.
I want to get an answer with an explanation, like why does recursion crash on 3k calls but GoTo
on 30k doesn't crash, and will it even fall into recursion? What does it depend on in GoTo
?
I've commented out unnecessary lines in your code that don't do anything:
Sub loopWork
'GoTo Label1
Label1:
if stateMainTimer = 2 Then
Exit Sub
End If
'GoTo Label2
'Label2:
loopWorkVoid()
GoTo label1
End Sub
Now it is clearly visible that this is the while
loop. The while
loop runs a little faster than the for
loop because there is no need for a counter. The while
loop isn't a recursion, no matter how you confuse it. The recursion would be if loopWork
procedure called itself or if loopWorkVoid
procedure called itself.
When one procedure/function calls another procedure/function or itself, the return address is written to the stack. Recursive calls cannot continue indefinitely, as this will inevitably lead to stack overflow. That is, recursive calls must be finite. And judging by your question, you used recursion, but didn't interrupt it in any way.
Here is a quote from the documentation:
Note:
The recursion levels are set at different levels based on the software platform. For Windows the recursion level is 5800. For Solaris and Linux, an evaluation of the stacksize is performed and the recursion level is calculated.
... but
GoTo
on 30k doesn't crash, and will it even fall into recursion?
It is impossible to give a definite answer to this question, because you haven't shown the implementation of procedure loopWorkVoid
. If procedure loopWorkVoid
calls procedure loopWork
, then yes, it will.
I provided an example code where
Label1
is called, and it callsLabel2
. ThenLabel2
callsLabel1
, and this goes around in a circle. Isn't this recursion?
No. This is not a recursion. What you meant should probably look something like this (but I'm not sure if this syntax is supported in LibreOffice BASIC
):
'Attention! Causes stack overflow!
Sub loopWork()
Dim Sub2 As Action
Dim Sub1 =
Sub()
'Label1
If stateMainTimer = 2 Then Exit Sub
Sub2()
End Sub
Sub2 =
Sub()
'Label2
loopWorkVoid()
Sub1()
End Sub
Sub1()
End Sub