FRE(-2) Returns Fixed Lowest Value Even After Stack Shrinks (40188)



The information in this article applies to:

  • Microsoft QuickBASIC 4.0
  • Microsoft QuickBASIC 4.0b
  • Microsoft QuickBASIC 4.5
  • Microsoft BASIC Compiler for MS-DOS and OS/2 6.0
  • Microsoft BASIC Compiler for MS-DOS and OS/2 6.0b
  • Microsoft Basic Professional Development System (PDS) for MS-DOS and MS OS/2 7.0
  • Microsoft Basic Professional Development System (PDS) for MS-DOS and MS OS/2 7.1

This article was previously published under Q40188

SUMMARY

The FRE(-2) function returns in bytes the amount of unused stack space available for the program.

However, when the stack-space allocation decreases, the value returned by FRE(-2) stays fixed at the smallest size that was unused by the stack up to that point of program execution. In other words, the values successively returned by FRE(-2) never increase -- they either decrease or stay the same.

This behavior is by design in the products listed above.

MORE INFORMATION

The sample program below demonstrates this behavior. The example given is a recursive subprogram that reports the value of FRE(-2) on the way down and back up through 10 levels of subprogram recursion. As the stack space increases with the level of recursion, the value of FRE(-2) decreases as expected. However, as the stack space decreases with lower levels of recursion, the value reported by FRE(-2) stays "bottomed out" at the lowest value reached during recursion.

The only way to reset the "bottomed-out" value returned by FRE(-2) is to end the program or execute a CLEAR statement. Note: CLEAR erases all variables and closes all files. In some cases, as shown in Example 2, adding an INPUT statement before the FRE(-2) allows FRE(-2) to go back up. Adding INPUT does not help Example 1.

Example 1

The following code example demonstrates a case in which FRE(-2) goes down and stays down:
   DECLARE SUB test (num)
   CLS
   PRINT "Before any calls, FRE(-2):"; FRE(-2)
   CALL test(0)
   END
   SUB test (num)
     PRINT "Call number"; num; "on the way DOWN, FRE(-2):"; FRE(-2)
     IF num < 9 THEN
       CALL test(num + 1)
     ELSE
       PRINT "Bottom of recursion reached."
     END IF
     PRINT "Call number"; num; "on the way UP, FRE(-2):"; FRE(-2)
   END SUB
				

Example 2

The following example demonstrates a case in which adding an INPUT statement lets the value returned by FRE(-2) go back up:
   DECLARE SUB mysub ()
   DEFINT A-Z
   CLS
   'INPUT k   ' Adding this INPUT statement makes FRE(-2) go back up.
   PRINT FRE(-2)
   a$ = "Test string"
   CALL mysub
   PRINT FRE(-2)
   END

   SUB mysub
   SHARED a$
   PRINT "hello "; a$
   END SUB
				

Modification Type:MinorLast Reviewed:8/16/2005
Keywords:KB40188