Microsoft KB Archive/40188

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

Article ID: 40188

Article Last Modified on 8/16/2005

-

APPLIES TO


 * Microsoft QuickBasic 4.0
 * Microsoft QuickBASIC 4.0b
 * Microsoft QuickBasic 4.5 for MS-DOS
 * Microsoft BASIC Compiler 6.0
 * Microsoft BASIC Compiler 6.0b
 * Microsoft BASIC Professional Development System 7.0
 * Microsoft BASIC Professional Development System 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 &quot;bottomed out&quot; at the lowest value reached during recursion.

The only way to reset the &quot;bottomed-out&quot; 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 &quot;Before any calls, FRE(-2):&quot;; FRE(-2) CALL test(0) END SUB test (num) PRINT &quot;Call number&quot;; num; &quot;on the way DOWN, FRE(-2):&quot;; FRE(-2) IF num < 9 THEN CALL test(num + 1) ELSE PRINT &quot;Bottom of recursion reached.&quot; END IF    PRINT &quot;Call number&quot;; num; &quot;on the way UP, FRE(-2):&quot;; 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$ = &quot;Test string&quot; CALL mysub PRINT FRE(-2) END

SUB mysub SHARED a$  PRINT &quot;hello &quot;; a$   END SUB

Additional query words: QuickBas BasicCom 4.00 4.00b 4.50 6.00 6.00b 7.00 7.10

Keywords: KB40188

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.