Q(uick)BASIC Statement: REDIM

Quick View

REDIM

A BASIC declaration that changes the space allocated to an array that has been declared $DYNAMIC

Worth knowing

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

Syntax
  • DIM [SHARED] variable[(subscripts)] [AS type] [,variable[(subscripts)] [AS type]]…
  • REDIM [SHARED] variable(subscripts) [AS type] [,variable(subscripts) [AS type]]…
Description/Parameter(s)
SHARED Specifies that variables are shared with all SUB or FUNCTION procedures in the module.
variable The name of an array or variable.
subscripts Dimensions of the array, expressed as follows:
[lower TO] upper [,[lower TO] upper]…
lowerThe lower bound of the array's subscripts. The default lower bound is zero.
upperThe upper bound.
AS type Declares the data type of the array or variable (INTEGER, LONG, SINGLE, DOUBLE, STRING, or a user-defined data type).
  • DIM declares either static or dynamic arrays. Unless array storage has been determined by $STATIC, $DYNAMIC, or COMMON, arrays dimensioned with numbers are static and arrays dimensioned with variables are dynamic. REDIM always declares dynamic arrays.
  • Static array storage is allocated when you start a program and remains fixed. Dynamic array storage is allocated while a program runs.
Example
' $DYNAMIC DIM A(49, 49) REDIM A(19, 14)
Syntax
  • REDIM [SHARED] variable(subscripts)[AS type] [,variable(subscripts)[AS type]]…
Description/Parameter(s)
Arguments Description
SHARED The optional SHARED attribute allows a module to share variables with all the procedures in the module; this differs from the SHARED statement, which affects only the variables within a single module. SHARED can only be used in REDIM statements in the module-level code.
variable A BASIC variable name.
subscripts The dimensions of the array. Multiple dimensions can be declared. The subscript syntax is described below.
AS type Declares variable as an elementary or user-defined type. The elementary types are INTEGER, LONG, SINGLE, DOUBLE, and STRING.

Subscripts in REDIM statements have the following form:

  • [lower TO] upper [,[lower TO] upper]…

The TO keyword provides a way to indicate both the lower and the upper bounds of an array's subscripts. The arguments lower and upper are numeric expressions specifying the lowest and highest value for the subscript. See the DIM statement details for more information about using the TO keyword.

The REDIM statement changes the space allocated to an array that has been declared $DYNAMIC.

When a REDIM statement is compiled, all arrays declared in the statement are treated as dynamic. At run time, when a REDIM statement is executed, the array is deallocated (if it is already allocated) and then reallocated with the new dimensions. Old array-element values are lost because all numeric elements are reset to 0, and all string elements are reset to null strings.

Note: Although you may change the size of an array's dimensions with the REDIM statement, you may not change the number of dimensions. For example, the following statements are legal:
  • ' $DYNAMIC
  • DIM A(50,50)
  • ERASE A
  • REDIM A(20,15) 'Array A still has two dimensions.
However, the following statements are not legal, and produce an error message that reads "Wrong number of dimensions":
  • ' $DYNAMIC
  • DIM A(50,50)
  • ERASE A
  • REDIM A(5,5,5) 'Changed number of dimensions from two to three.
Example

This example shows how to use REDIM to allocate an array of records and then how to free the memory that the records use.

TYPE KeyElement Word AS STRING * 20 Count AS INTEGER END TYPE ' Make arrays dynamic. ' $DYNAMIC CLS ' Clear screen ' Allocate an array of records when you need it. REDIM Keywords(100) AS KeyElement Keywords(99).Word = "ERASE" Keywords(99).Count = 2 PRINT "Keyword 99 is "; Keywords(99).Word PRINT "Count is"; Keywords(99).Count ' Free the space taken by Keywords when you're finished. ERASE Keywords END

Sample Output:

Keyword 99 is ERASE Count is 2
Syntax
  • REDIM [SHARED | PRESERVE] variable(subscripts) [AS type] [,variable(subscripts) [AS type]]…
Description/Parameter(s)
SHARED Allows a module to share variables with all the procedures in the module.
PRESERVE Preserves the data in an existing array.
variable A BASIC variable name.
subscripts The dimensions of the array (see Details).
AS type Declares a data type for the array (see Details).
  • The SHARED attribute differs from the SHARED statement, which affects only the variables within a single module. SHARED can be used in REDIM statements only in the module-level code.
  • PRESERVE preserves data when changing the outer bound of the array.
  • subscripts are the dimensions of the array. Multiple dimensions can be declared. The subscript syntax is:
    • [lower TO] upper [,[lower TO] upper]…
    lower TO upperIndicates the lower and upper bounds of an array's subscripts. Lower and upper are numeric expressions that specify the lowest and highest value for the subscript. See the DIM statement details for more information about using the TO keyword.
  • AS type declares the type of the variable. The type may be INTEGER, LONG, SINGLE, DOUBLE, STRING (for variable-length strings), STRING * length (for fixed-length strings), CURRENCY, or a user- defined type.

New Features

  • The PRESERVE keyword preserves data in the array, allowing you to allocate space to arrays more efficiently.
  • BASIC now supports the CURRENCY data type (type suffix @). This is used in the AS type clause of REDIM.
  • BASIC now supports static arrays in user-defined types.

Usage Notes

  • The REDIM statement changes the space allocated to an dynamic array.
  • When a REDIM statement is compiled, all arrays declared in the statement are treated as dynamic. At run time, when a REDIM statement is executed, the array is deallocated (if it is already allocated) and then reallocated with the new dimensions. If you do not use PRESERVE, old array-element values are lost.
  • The PRESERVE keyword allows you to raise or lower the outer bound of a dynamic array without erasing data. For example:
    • REDIM X(10, 10, 10)
    • REDIM PRESERVE X(10, 10, 15)
    This example keeps any data entered in the array X() and adds space for more elements in the third dimension of the array. The third dimension of X() is the rightmost bound of the array. To change the leftmost bound, you must compile the program with /R.

Important

  • Although you can change the size of an array's dimensions with the REDIM statement, you cannot change the number of dimensions. For example, the following statements are legal:
    • ' $DYNAMIC
    • DIM A(50,50)
    • ERASE A
    • REDIM A(20,15) 'Array A still has two dimensions.
    However, the following statements are not legal, and produce the error message: "Wrong number of dimensions":
    • ' $DYNAMIC
    • DIM A(50,50)
    • ERASE A
    • REDIM A(5,5,5) 'Changed number of dimensions from two to three.
Examples

Example 1 uses the REDIM statement to allocate an array of records. It hen frees the memory that the records use with the ERASE statement.
Example 2 uses the PRESERVE keyword in the REDIM statement to add records to an existing array as more data is entered.

Example 1

TYPE KeyElement Word AS STRING * 20 Count AS INTEGER END TYPE 'Allocate an array of records when you need it. REDIM Keywords(100) AS KeyElement Keywords(99).Word = "ERASE" Keywords(99).Count = 2 CLS PRINT "Keyword 99 is "; Keywords(99).Word PRINT "Count is"; Keywords(99).Count 'Free the space taken by Keywords when you're finished. ERASE Keywords END

Sample Output:

Keyword 99 is ERASE Count is 2

Example 2

DATA 23, -45, 6, 5, 1,12, 7, 1, -9,0 CLS REDIM X(1 TO 10) AS INTEGER ' Create dynamic array. FOR I% = 1 TO 10 ' Read in data. READ X(I%) NEXT I% DO INPUT "Item to add to list (ENTER to finish)"; a$ ' Resize IF LEN(a$) THEN ' array X() as items REDIM PRESERVE X(1 TO UBOUND(X) + 1) AS INTEGER ' are added using X(UBOUND(X)) = VAL(a$) ' REDIM PRESERVE. END IF LOOP WHILE LEN(a$) FOR I% = 1 TO UBOUND(X) ' Print array. PRINT X(I%) NEXT I%