Article ID: 101880
Article Last Modified on 8/16/2005
' show number of free bytes far heap: PRINT FRE(-1) ' show number of free bytes near heap: PRINT STACK ' for VB-DOS and Basic PDS PRINT FRE(0) ' for QuickBasicIf you find that you have the same amount of free hear and far heap space, far heap is depleted. You should print these values as near as possible to the point in your program immediately before the "Out of memory" error occurs.
DIM a(1000) AS INTEGER ' static array, stored in near heap REM $DYNAMIC DIM b(1000) AS INTEGER ' dynamic array, stored in far heapConvert arrays of variable-length strings into arrays of fixed-length strings to allow REM $DYNAMIC to store this data in far heap. For example:
' example arrays of variable-length strings DIM a$(2000) DIM b(2000) AS STRING ' example array of fixed-length strings, up to length 16 each DIM c(2000) AS STRING * 16Use the RTRIM$ function to remove trailing spaces from fixed-length strings when you retrieve them from the array. For example
a$(1) = "hello" PRINT "<" + a$(1) + ">" ' prints <hello> c(1) = "hello" PRINT "<" + c(1) + ">" ' prints <hello > PRINT "<" + RTRIM$(c(1)) + ">" ' prints <hello>Using fixed-length arrays typically increases the storage required for each string because space is reserved for the maximum possible length. If you require an array larger than 64K, invoke the interpreter or compiler with the /Ah option. Otherwise you receive "Subscript out of range," error 9.
VBDOS /Ah -- Visual Basic for MS-DOS
QBX /Ah -- Basic PDS
QB /Ah -- Microsoft QuickBasic
BC /Ah ... -- compiler for all 3 products
DIM a(8000) AS STRING * 17 ' causes error "Subscript out of range" DIM b(8000) AS STRING * 16 ' okayCreate arrays of one element out of large variables that are not already arrays and are not variable-length strings. This causes REM $DYNAMIC to store them in far heap rather than near heap. User defined type variables and fixed-length strings are good candidates. For example,
TYPE aType
s AS STRING * 1000
a(100) AS LONG
END TYPE
DIM t1 AS aType ' stored in near heap
t1.s = "hello"
PRINT t1.s
DIM t2(0) AS aType ' stored in far heap
t2(0).s = "hello"
PRINT t2(0).s
DIM f1 AS STRING * 2000 ' stored in near heap
DIM f2(0) AS STRING * 2000 ' stored in far heap
After adding REM $DYNAMIC to a support module (a .BAS file other than
your start-up file), you may encounter a side effect problem with the
symptom of error "Subscript out of range." This happens on arrays
defined with DIM SHARED at the module level of a support module.
Similarly, after adding REM $DYNAMIC to a form, you may encounter the
error "Executable code not allowed in module level of a form." These
errors are due to REM $DYNAMIC changing DIM SHARED from a compile-time
statement to a run-time statement for arrays.
REM $DYNAMIC
DIM SHARED b() AS INTEGER
SUB s ()
STATIC is_alloc AS INTEGER
IF NOT is_alloc THEN
DIM b(1000) AS INTEGER
is_alloc = -1 ' true
END IF
b(100) = 123
PRINT b(100)
END SUB
To work around the Visual Basic error "Executable code not allowed in
module level of a form," define the array at the module level and use
REDIM to allocate the array from the Form_Load event handler.
Additional query words: VBmsdos QuickBas BasicCom B_VBmsdos 1.00 b_basiccom 7.10 b_quickbas 4.50
Keywords: KB101880