KnowledgeBase Archive

An Archive of Early Microsoft KnowledgeBase Articles

View on GitHub

Q35658: "Array Already Dimensioned" if 2nd DIM for Static Array in IF

Article: Q35658
Product(s): See article
Version(s): 1.00 1.01 1.02 2.00 2.01 3.00 4.00 4.00b 4.50
Operating System(s): MS-DOS
Keyword(s): ENDUSER | B_BasicCom | mspl13_basic
Last Modified: 12-DEC-1989

Static arrays are dimensioned at compile time, regardless of whether
or not the DIM statement is in the program flow of control. For
example, if the DIM for a static array occurs in an IF statement that
would never be executed at run time, the array will still be
dimensioned at compile time. If you attempt to dimension a given
static array more than once in a source file, you will get an "Array
Already Dimensioned" error on the second DIM statement at compile
time, as in the following example:

10 x = 1
20 IF x = 2 THEN DIM array(14)
25 IF x = 1 THEN DIM array(25)  ' "Array Already Dimensioned" compile-time
30 array(21) = 4
40 PRINT array(21)

This behavior occurs in all versions of Microsoft QuickBASIC for the
IBM PC, in the Microsoft BASIC Compiler Versions 5.35 and 5.36 for
MS-DOS and Versions 6.00 and 6.00b for MS-DOS and MS OS/2, and in
Microsoft BASIC PDS Version 7.00 for MS-DOS and MS OS/2.

The QuickBASIC compiler allocates static arrays as they are
encountered in one top-to-bottom pass through the source file at
compile time. In contrast, dynamic arrays are dimensioned only when
the flow of control reaches a DIM statement at run time.

By default, arrays are static in the above compilers, unless you DIM
them with a variable in the subscript. Adding the REM $DYNAMIC
metacommand to the top of the program makes the arrays default to
dynamic. Note that arrays are always dynamic in the Microsoft
GW-BASIC, IBM BASICA, and Compaq BASICA Interpreters.

The program below is another illustration. The compiled program will
run without any error message, since the DIM for the static array is
allocated at compile time. If you added a REM $DYNAMIC statement to
make all arrays default to dynamic, the compiled program would give a
"Subscript out of Range" error message at run time:

10 x = 1
20 IF x = 2 THEN DIM array(14)
30 array(11) = 4
40 PRINT array(11)

The above program will give a "Subscript out of Range" error in the
GW-BASIC Version 3.20 interpreter, since the DIM for the dynamic array
in the IF statement is not executed at run time, and the array in line
30 defaults to only ten elements.

THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Copyright Microsoft Corporation 1986-2002.