English Amiga Board


Go Back   English Amiga Board > Coders > Coders. General

 
 
Thread Tools
Old 07 May 2003, 18:04   #1
Tony Landais
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 ?
Tony Landais is offline  
Old 07 May 2003, 20:22   #2
Chuckles
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!
Chuckles is offline  
Old 07 May 2003, 21:24   #3
BippyM
Global Moderator
 
BippyM's Avatar
 
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
BippyM is offline  
Old 07 May 2003, 22:41   #4
SabreGolly
Registered User
 
SabreGolly's Avatar
 
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.
SabreGolly is offline  
Old 07 May 2003, 22:46   #5
SabreGolly
Registered User
 
SabreGolly's Avatar
 
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...
SabreGolly is offline  
Old 07 May 2003, 23:21   #6
Chuckles
The Ancient One
 
Join Date: Feb 2002
Location: Kansas City/USA
Age: 69
Posts: 685
Re: Blitz Basic - Gosub - Stack Overflow

Quote:
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!
Chuckles is offline  
Old 07 May 2003, 23:21   #7
RCK
Administrator
 
RCK's Avatar
 
Join Date: Feb 2001
Location: Paris / France
Age: 45
Posts: 3,091
rating ?! lol I forgot to remove it in this section - updated.
RCK is offline  
Old 08 May 2003, 01:24   #8
Severin
Registered User
 
Severin's Avatar
 
Join Date: Dec 2002
Location: Gloucester / UK
Posts: 700
Send a message via ICQ to Severin
Quote:
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
Severin is offline  
Old 08 May 2003, 11:02   #9
SabreGolly
Registered User
 
SabreGolly's Avatar
 
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!!!
SabreGolly is offline  
Old 08 May 2003, 19:19   #10
BippyM
Global Moderator
 
BippyM's Avatar
 
Join Date: Nov 2001
Location: Derby, UK
Age: 48
Posts: 9,355
Ctrl-C <I think!>
BippyM is offline  
Old 08 May 2003, 20:35   #11
SabreGolly
Registered User
 
SabreGolly's Avatar
 
Join Date: Apr 2003
Location: Cheshire/UK
Age: 52
Posts: 77
Quote:
Ctrl-C <I think!>
Thank You! Thank You! Thank You!

I'll try it when I get home from work...
SabreGolly is offline  
 


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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 10:58.

Top

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Page generated in 0.08568 seconds with 13 queries