Q(uick)BASIC Metacommand: $STATIC
Quick View
$STATIC, $DYNAMIC Metacommands
$DYNAMIC sets aside storage for arrays while the program is running. $STATIC sets aside storage for arrays during compilation
Worth knowing
Useful and cross-version information about the programming environments of QBasic and QuickBasic.
Syntax
- {REM | '} $STATIC
- {REM | '} $DYNAMIC
Description/Parameter(s)
{REM | '} | REM or a remark character (') must precede metacommands. |
$STATIC | Specifies that arrays declared in subsequent DIM statements are static arrays (unless they are declared in a non-static SUB or FUNCTION procedure). Array storage is allocated when you start the program, and remains fixed. |
$DYNAMIC | Specifies that arrays declared in subsequent DIM statements are dynamic arrays. Array storage is allocated dynamically while the program runs. |
DIM and REDIM usually provide a better way to specify whether arrays are dynamic or static. |
See also:
Syntax
- REM $STATIC or ' $STATIC
- REM $DYNAMIC or ' $DYNAMIC
Description/Parameter(s)
The $STATIC and $DYNAMIC metacommands tell the compiler how to allocate memory for arrays. Neither of these metacommands takes an argument:.
- 'Make all arrays dynamic.
- '$DYNAMIC
$STATIC sets aside storage for arrays during compilation. When the $STATIC metacommand is used, the ERASE statement reinitializes all array values to zero (numeric arrays) or the null string (string arrays) but does not remove the array. The REDIM statement has no effect on $STATIC arrays.
$DYNAMIC allocates storage for arrays while the program is running. This means that the ERASE statement removes the array and frees the memory it took for other uses. You can also use the REDIM statement to change the size of a $DYNAMIC array.
The $STATIC and $DYNAMIC metacommands affect all arrays except implicitly dimensioned arrays (arrays not declared in a DIM statement). Implicitly dimensioned arrays are always allocated as if $STATIC had been used.
Example
See the ⮜ ERASE Statement ⮞ programming example, which demonstrates uses of the $DYNAMIC and $STATIC commands.
See also:
Syntax
- REM $STATIC or ' $STATIC
- REM $DYNAMIC or ' $DYNAMIC
Description/Parameter(s)
When the $STATIC metacommand is used, the ERASE statement reinitializes all array values to zero (numeric arrays) or the null string (string arrays) but does not remove the array.
$DYNAMIC allocates storage for arrays while the program is running. This means that the ERASE statement removes the array and frees the memory it took for other uses. You also can use the REDIM statement to change the size of an array allocated using $DYNAMIC.
The $STATIC and $DYNAMIC metacommands affect all arrays except implicitly dimensioned arrays (arrays not declared in a DIM statement). Implicitly dimensioned arrays are always allocated as if $STATIC had been used.
All arrays inside a SUB or FUNCTION procedure are dynamic unless the STATIC keyword is included in the SUB or FUNCTION statement.
Example
This example uses the ERASE statement with the $DYNAMIC and $STATIC metacommands:
REM $DYNAMIC
DIM A(100, 100)
'Deallocates array A.
ERASE A
'Redimension array A.
REDIM A(5, 5)
REM $STATIC
DIM B(50, 50)
'This sets all elements of B equal to zero.
'B still has the dimensions assigned by DIM.
ERASE B
See also: