Function to Show How Much DGROUP Remains in Basic PDS 7.0/7.1 (79028)



The information in this article applies to:

  • Microsoft Basic Professional Development System (PDS) for MS-DOS and MS OS/2 7.1
  • Microsoft Basic Professional Development System (PDS) for MS-DOS and MS OS/2 7.0

This article was previously published under Q79028

SUMMARY

You may need to know exactly how much room is left in DGROUP (the default 64K data segment) before DGROUP fills up. Below is a function that returns a close approximation of the amount of space left in DGROUP.

MORE INFORMATION

The STACK function returns the amount of space in DGROUP that the stack has left to grow before it collides with the program's data. While this number is useful, at times it seems inaccurate, as the number could be large when an "Out of String Space" message occurs.

The reason for this appearance of inaccuracy is that the boundary between the stack and the data is fixed only from the data side. That is, the data is not allowed to grow into the reserved stack space (which is 3K by default, but is resizable with the STACK statement), but the stack is allowed to grow beyond the set stack boundary until it collides with the data.

Below is a function that reports a more accurate estimation of the space left in DGROUP. Note that it will not be exact because it uses the FRE(-2) function, which reflects the least amount of stack space there has been during the entire execution of the program. If the function below is called from the deepest level of subroutine calls, it will be more accurate than it would be, for example, if it were called at the end of the program.

The equation derived below is based on the fact that the STACK function does not account for the reserved stack space. FRE(-2) returns how much room (approximately) is left on the stack. Subtracting the two shows approximately the space between the data and the reserved stack space boundary. In the case that FRE(-2) returns a negative number, indicating that the stack has grown beyond the set stack size, the STACK function itself returns an accurate number.

Code Example

DECLARE FUNCTION RemainingDGROUP& ( )
CONST StackSize = 4000    ' Can be any size needed
STACK StackSize
...
'Other program code
...
If RemainingDGROUP& > 6000 then
' Check if there's enough data space to complete operation
     CALL ...
ELSE
' If not, print message
     PRINT "Not enough memory to ..."
ENDIF
END

'Function code:

FUNCTION RemainingDGROUP&
     Temp& = FRE(-2)
     IF Temp& > 0 then
          RemainingDGROUP& = STACK - Temp&
     ELSE
          RemainingDGROUP& = STACK
     ENDIF
END FUNCTION
				

Modification Type:MajorLast Reviewed:10/20/2003
Keywords:KB79028