07 May 2003, 18:04 | #1 |
Zone Friend
Join Date: Mar 2003
Location: uk
Posts: 426
|
Blitz Basic - Gosub - Stack Overflow
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 ? |
07 May 2003, 20:22 | #2 |
The Ancient One
Join Date: Feb 2002
Location: Kansas City/USA
Age: 69
Posts: 685
|
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! |
07 May 2003, 21:24 | #3 |
Global Moderator
Join Date: Nov 2001
Location: Derby, UK
Age: 48
Posts: 9,355
|
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 |
07 May 2003, 22:41 | #4 |
Registered User
Join Date: Apr 2003
Location: Cheshire/UK
Age: 52
Posts: 77
|
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. Last edited by SabreGolly; 07 May 2003 at 22:54. |
07 May 2003, 22:46 | #5 |
Registered User
Join Date: Apr 2003
Location: Cheshire/UK
Age: 52
Posts: 77
|
Sorry - Rating!
Accidently put in 'worst' rating - those damn scroll wheels on mice - arse! Sorry...
|
07 May 2003, 23:21 | #6 | |
The Ancient One
Join Date: Feb 2002
Location: Kansas City/USA
Age: 69
Posts: 685
|
Re: Blitz Basic - Gosub - Stack Overflow
Quote:
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! |
|
07 May 2003, 23:21 | #7 |
Administrator
Join Date: Feb 2001
Location: Paris / France
Age: 45
Posts: 3,091
|
rating ?! lol I forgot to remove it in this section - updated.
|
08 May 2003, 01:24 | #8 | |
Registered User
|
Quote:
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 |
|
08 May 2003, 11:02 | #9 |
Registered User
Join Date: Apr 2003
Location: Cheshire/UK
Age: 52
Posts: 77
|
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!!! |
08 May 2003, 19:19 | #10 |
Global Moderator
Join Date: Nov 2001
Location: Derby, UK
Age: 48
Posts: 9,355
|
Ctrl-C <I think!>
|
08 May 2003, 20:35 | #11 | |
Registered User
Join Date: Apr 2003
Location: Cheshire/UK
Age: 52
Posts: 77
|
Quote:
I'll try it when I get home from work... |
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
Thread Tools | |
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Blitz basic 2 Help | Havie | Coders. Blitz Basic | 30 | 08 September 2013 09:15 |
"Stack overflow": any way to solve that problem? | Paulisse | New to Emulation or Amiga scene | 10 | 27 January 2008 13:45 |
blitz basic | petza | request.Apps | 11 | 08 April 2007 01:49 |
Blitz Basic 2 anyone? | jobro | request.Apps | 12 | 28 November 2005 18:15 |
Blitz Basic 2 | LaundroMat | Retrogaming General Discussion | 5 | 24 July 2001 08:10 |
|
|