Q(uick)BASIC Statement: ERASE

Quick View

ERASE

A memory management statement that reinitializes the elements of static arrays or deallocates dynamic arrays

Worth knowing

Useful and cross-version information about the programming environments of QBasic and QuickBasic.

Syntax
  • ERASE arrayname [,arrayname]…
Description/Parameter(s)
arrayname The name of an array.
  • For static arrays, ERASE sets each element of a numeric array to zero and each element of a string array to null.
  • For dynamic arrays, ERASE frees the memory used by the array. You must redeclare the array's dimensions with REDIM or DIM before using it.
Example
DIM a%(0) a%(0) = 6 PRINT "Before: "; a%(0) ERASE a% PRINT "After: "; a%(0)
Syntax
  • ERASE arrayname [,arrayname…]
Description/Parameter(s)

The arrayname arguments are the names of arrays to erase. ERASE has different effects on $STATIC and $DYNAMIC arrays.

The ERASE statement sets the elements of a $STATIC array to zeros in the case of a numeric array or null strings ("") in the case of a string array. If the array is an array of records, the ERASE statement sets all elements of each record to zeros, including fixed-string elements.

However, using ERASE on a $DYNAMIC array frees the memory used by the array. Before your program can refer to the $DYNAMIC array again, it must first redimension the array with a DIM or REDIM statement. Redimensioning an array with a DIM statement without first erasing it produces a duplicate definition run-time error message that reads Array already dimensioned. The ERASE statement is not required when arrays are redimensioned with REDIM.

Example

This example shows the use of ERASE with the $DYNAMIC and $STATIC metacommands:

REM $DYNAMIC DIM A(100, 100) 'This 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
Syntax
  • ERASE arrayname [,arrayname]…
Description/Parameter(s)

Usage Notes

  • ERASE sets the elements of an array as follows:
Type of array ERASE sets array elements set to
Numeric static array Zeros.
String static array Null strings ("").
Array of records Zeros - all elements of each word, including fixed-string elements.
  • Using ERASE on a dynamic array frees the memory used by the array. Before your program can refer to the dynamic array again, it must first redeclare the array's dimensions with a DIM or REDIM statement. If you redeclare the array's dimensions with a DIM statement without first erasing it, BASIC generates the run-time error message, "Array already dimensioned." The ERASE statement is not required when dimensions are redeclared with REDIM.
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