View Full Version : Blitz Basic - Gosub - Stack Overflow
Tony Landais
07 May 2003, 18:04
I use the command gosub and it creates a stack overflow, crashes, guru....
By what should I replace that command? or is there an intelligent way to use it ? :)
Chuckles
07 May 2003, 20:22
I've never coded in Blitz Basic, but have done plenty of coding in other variants of basic over time. There's nothing inherently wrong with using a gosub - in fact it is a fine idea to the extent that it leads to code that is more structured. The stack overflow is more than likely a result of either an overuse, a misuse, or of recursion run amok. Any time you perform a gosub or the equivalent in any language, some information is pushed onto the stack in order to support a return to the next instruction when a return is done from the subroutine. The return instruction removes that information from the stack in the process of doing the return. If the space allocated to the stack becomes filled with this information, the next gosub causes the "stack overflow" to occur.
One reason this might happen is having a combination of a small stack and gosubs which are too deeply nested. In that sort of case, you can reallocate more space for the stack if you really need to have your gosubs so deeply nested, but a better approach is usually to rethink your approach so as to not require such deep nesting in the first place.
A possible coding error which can result in this error would be to use a gosub to call a subroutine, but to exit the subroutine via a goto rather than a return. This leaves all of the information pushed onto the stack there, and eventually the stack fills up when the subroutine has been called enough times. Depending on the language, the proper way to exit may be "return", "exitsub" or some other instruction. Not having used Blitz, I'm not sure what the proper instruction is in this case.
Recursion is another way that stack overflows can occur, though not all languages allow recursion, and I don't know if Blitz does or not. In recursion a subroutine would contain a gosub to itself, and if it does so enough times before returning back up the tree, you get a stack overflow that way. This often happens due to a faulty test for the condition that should cause the subroutine to exit.
Hopefully, this may help you to discover the source of your own stack overflow problems. Good luck!
What is the syntax you are using?
With blitz makesure you do one of the following:
gosub foldername
.foldername
<do my subroutine stuff>
Return
or
JSR foldername
.foldername
<Do subroutine stuff>
RTS
SabreGolly
07 May 2003, 22:41
You must finish your subroutine with 'Return'. If you don't the subroutine info remains in the stack, and the next time it is called the stack is added to. Within minutes the stack is full - and DEATH:kill !
if you must leave a subroutine without 'return' use the POP GOSUB command. It removes the last gosub info from the stack eg.
FOR f=1 TO 1000
GOSUB stuff
andback:
NEXT
END
stuff:
IF (something happens) THEN GOTO somewhere
RETURN
somewhere:
blah blah
GOTO andback
If 'something happens' is true you will end up with 1000 bits of info on the stack. add...
IF something happens THEN POP GOSUB: GOTO somewhere
you must do the same for for/next loops, select, while, repeat loops.
Sorry the ask something else on your topic mate, but which key combination is used to bring up the debugger in blitz mode? I've just returned to Blitz Basic and can't find the user manual! (I asked this on another thread http://eab.abime.net/showthread.php?threadid=9699
Cheers.
SabreGolly
07 May 2003, 22:46
Accidently put in 'worst' rating - those damn scroll wheels on mice - arse! Sorry...
Chuckles
07 May 2003, 23:21
Originally posted by Tony Landais
or is there an intelligent way to use it ? :)
I must admit that the above quote triggered a chuckle here as I imagined a hypothetical user calling in this question to a hypothetical Blitz Basic Customer Support Representative:
Caller: My programs blow up when I use the GOSUB command. Is there an intelligent way to use it?
CS Rep: No sir, I'm afraid not. That command was specifically designed to be used only in completely moronic ways, actually. The previous version of the product didn't have it, and it was one of the features that our users most demanded, so we added it to the new version.
Ok, maybe my sense of humor IS a bit warped - so sue me! :laughing
rating ?! lol I forgot to remove it in this section - updated.
Severin
08 May 2003, 01:24
You must finish your subroutine with 'Return'. If you don't the subroutine info remains in the stack, and the next time it is called the stack is added to. Within minutes the stack is full - and DEATH !
Yep, I've seen the code, 4 or 5 routines all with a gosub at the bottom instead of a goto and not a single return :( I've re-written it and returned it to Tony, hope it helps :)
basically it was really one long subroutine, with no controlling main routine. Tony, next time try something like this (it's much easier)...
wbstartup type stuff
statements & functions
initalise variables etc
main loop
lots of subroutines down here
data statements, incbin etc at the end
eg.
WBStartup
CloseEd
Statement BungToScreen{text$}
print text$
End Statement
test$="Hello World!"
.Main
Repeat
Gosub DoSomething
VWait 10
until CtrlC
End
.DoSomething
For i = 1 to 5
BungToScreen {test$}
Next
NPrint ""
Return
SabreGolly
08 May 2003, 11:02
I agree - goto's and gosub's are not really required if you use FUNCTIONs and STATEMENTs - which are a much more pleasant way to program!
Sorry to ask again - but I'm desperate...
Which keys call up the debugger in Blitz mode?!!! Help!!!
SabreGolly
08 May 2003, 20:35
Ctrl-C <I think!>
Thank You! Thank You! Thank You!
I'll try it when I get home from work...
vBulletin® v3.7.0, Copyright ©2000-2013, Jelsoft Enterprises Ltd.