Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

OUT OF STACK SPACE is in our way!

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
We at Titan Software are currently making a military strategy game for Qbasic, but we keep getting the message "Out of stack space." WHAT DO WE DO?

Also, if anyone has any solution to the Out Of Memory problem, that'd be helpful too. If you need any more info let me know.
 
Out of Stack Space can be caused by two things: recursive functions and error handlers (ON ERROR GOTO).

To fix it, try this
CLEAR ,,32767

Note that this clears all of the memory variables, so you better put this before you declare any arrays. If it doesn't work, then you need to rewrite your program, so that it doesn't recurse so much (and use tons of stack space).

 
Mr. Titan Software, it sounds as though you're using subroutines for every part of the program's functionality, and simply calling the next subroutine from each subroutine to move around (e.g., inside menus, etc.). If this is the case, your approach needs to be modified. My suggestion is that you change your SUBs into FUNCTIONs, and assign each of those FUNCTIONs a number. Then, have your main program look at the value that each function returns and use that to determine what the next FUNCTION to call is. Finally, instead of calling the FUNCTIONs from inside the FUNCTIONs, simply return the value corresponding to the next function to call. Example:

Old (broken) way:
[tt]
DECLARE SUB choice1 ()
DECLARE SUB choice2 ()
DECLARE SUB menu1 ()
DECLARE SUB menu2 ()
DECLARE SUB promptForValue (a$)

'Main program
menu1

SUB choice1
PRINT "You selected choice #1!"
menu2
END SUB

SUB
choice2
PRINT "You selected choice #2!"
menu2
END SUB

SUB
menu1
DO
PRINT
"1. Another menu"
PRINT "2. Exit program"
PRINT
promptForValue a$
SELECT CASE VAL(a$)
CASE 1: menu2
CASE 2: END
END SELECT
LOOP
END SUB

SUB
menu2
DO
PRINT
"1. Choice 1"
PRINT "2. Choice 2"
PRINT "3. Return to main menu"
PRINT
promptForValue a$
SELECT CASE VAL(a$)
CASE 1: choice1
CASE 2: choice2
CASE 3: menu1
END SELECT
LOOP
END SUB

SUB
promptForValue (a$)
LINE INPUT "Enter a selection: ", a$
END SUB
[/tt]

New (working, better, more modular) way:
[tt]
DECLARE FUNCTION choice1% ()
DECLARE FUNCTION choice2% ()
DECLARE FUNCTION menu1% ()
DECLARE FUNCTION menu2% ()
DECLARE FUNCTION promptForValue$ (a$)
CONST SELECTchoice1% = 1
CONST SELECTchoice2% = 2
CONST SELECTmenu1% = 3
CONST SELECTmenu2% = 4
CONST SELECTendProgram% = 5

'Main program
selection% = SELECTmenu1%
DO
SELECT CASE
selection%
CASE SELECTchoice1%:
selection% = choice1%
CASE SELECTchoice2%:
selection% = choice2%
CASE SELECTmenu1%:
selection% = menu1%
CASE SELECTmenu2%:
selection% = menu2%
CASE SELECTendProgram%:
EXIT DO
END SELECT
LOOP
END

FUNCTION
choice1%
PRINT "You selected choice #1!"
choice1% = SELECTmenu2%
END FUNCTION

FUNCTION
choice2%
PRINT "You selected choice #2!"
choice2% = SELECTmenu2%
END FUNCTION

FUNCTION
menu1%
DO
PRINT
"1. Another menu"
PRINT "2. Exit program"
PRINT
promptForValue a$
SELECT CASE VAL(a$)
CASE 1:
menu1% = SELECTmenu2%
EXIT FUNCTION
CASE
2:
menu1% = SELECTendProgram%
EXIT FUNCTION
END SELECT
LOOP
END FUNCTION

FUNCTION
menu2%
DO
PRINT
"1. Choice 1"
PRINT "2. Choice 2"
PRINT "3. Return to main menu"
PRINT
promptForValue a$
SELECT CASE VAL(a$)
CASE 1:
menu2% = SELECTchoice1%
EXIT FUNCTION
CASE
2:
menu2% = SELECTchoice2%
EXIT FUNCTION
CASE
3:
menu2% = SELECTmenu1%
EXIT FUNCTION
END SELECT
LOOP
END FUNCTION

SUB
promptForValue (a$)
LINE INPUT "Enter a selection: ", a$
END SUB
[/tt]

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top