Commodore‎ > ‎BASIC‎ > ‎Keywords‎ > ‎

DISPOSE

Keyword Abbreviation Token (hex) Version(s) Classification
DISPOSE DI{Shift+S} E4 4.7 Statement
SSHAPE S{Shift+S} E4 3.5, 7.0 Command and Statement

 
Syntax  
DISPOSE { FOR | GOSUB }
 
 
Purpose  
Stack management.  Remove unwanted "tokens" from the BASIC stack.

 
 
Remarks  
DISPOSE is used to "clean" the BASIC stack of FOR and GOSUB entries ("tokens"), usually caused by the use of GOTO.  If unused "tokens" are left on the stack repeatedly, an OUT OF MEMORY ERROR will eventually occur (a "memory leak").
 
DISPOSE must be followed by either the keyword FOR or else the keyword GOSUB.  Otherwise a SYNTAX ERROR occurs.  BASIC will then scan its stack for the most recent entry of the specified type (FOR or GOSUB).  If not found, then UNABLE TO DISPOSE (without the word ERROR) occurs.  Otherwise, all entries starting from the most recent back to (and including) the found entry will be removed from the BASIC stack.  Unlike NEXT, which allows a variable name to be given to "dispose" of possibly multiple FOR entries, DISPOSE will not allow an a variable name.  Thus you may need to use DISPOSE multiple times.
 
Unfortunately this handy statement is only available in BASIC v4.7.  I'm not sure why it isn't available in later versions; it may be because this is considered bad programming style, and because newer versions (not neccessarily a greater version #) provide DO/LOOP which can be cleanly escaped with EXIT.  However, that doesn't solve the problem of FOR/NEXT.  Maybe they want you to use DO/LOOP instead?  But that can be slower and use more program memory.  Also, it is often more effecient to DISPOSE of a GOSUB entry than to maintain a bunch of state flags within a program.  This has a lot to do with the lack of exception-handling statements in CBM BASIC.  Some ML utilities have been published for other machines to implement the behavior of DISPOSE, because it generally is faster than the alternatives.
 
Example:
NEW
10 FOR X=1 TO 10000
20 REM do something useful here
30 GET A$
40 IF LEN(A$) THEN DISPOSE FOR : GOTO 60
50 NEXT
RUN
       User presses SPACE-BAR after a second or so... 
READY.
PRINT X
 700

READY.
 
In that example, the "user abort" command (pressing any key while in the loop) escapes from the FOR/NEXT loop and cleans the stack.  In this example, it would also be possible to clean the stack with X=10000:NEXT (because X=10000 is the terminating condition of FOR), however that would destroy the value of X which might be needed.  Of course the value of X could be saved to another variable.  But those alternatives are pretty kludgy compared to DISPOSE.  Similar cases involving GOSUB are nearly imposible to solve without DISPOSE unless you maintain one or more state variables in your program.
 
  Compare With  
  Contrast With  
  See Also  
CLR 

© H2Obsession, 2014
Comments