Microsoft KB Archive/67373

= CLEAR Can Cause &quot;Subscript out of Range&quot; with $DYNAMIC Array =

Article ID: 67373

Article Last Modified on 8/16/2005

-

APPLIES TO


 * Microsoft QuickBasic 4.0
 * Microsoft QuickBASIC 4.0b
 * Microsoft QuickBasic 4.5 for MS-DOS
 * Microsoft BASIC Compiler 6.0
 * Microsoft BASIC Compiler 6.0b
 * Microsoft BASIC Professional Development System 7.0
 * Microsoft BASIC Professional Development System 7.1
 * Microsoft GW-BASIC 3.2
 * Microsoft GW-BASIC 3.22
 * Microsoft GW-BASIC 3.23

-



This article was previously published under Q67373



SUMMARY
Increasing the stack space with the CLEAR statement also reinitializes all variables. This includes erasing the memory for any $DYNAMIC arrays and initializing the elements of a $STATIC array to zero or null strings. For this reason, CLEAR should normally be used only as the very first executable statement in a program. Using it anywhere else can cause problems. For example, once you have used CLEAR, if you attempt to access an element of a $DYNAMIC array, the error &quot;Subscript out of range&quot; will be generated. This is expected behavior because the CLEAR statement deallocates all memory for the dynamic array. After a CLEAR, all $DYNAMIC arrays must be re-created with the REDIM statement.

This information applies to Microsoft QuickBasic versions 2.0, 2.01, 3.0, 4.0, 4.0b, and 4.5 for MS-DOS; Microsoft Basic Compiler versions 6.0 and 6.0b for MS-DOS and MS OS/2; Microsoft Basic Professional Development System (PDS) versions 7.0 and 7.1 for MS-DOS and MS OS/2; and all versions of GW-Basic (such as 3.2, 3.22, and 3.23) for MS-DOS.



MORE INFORMATION
Note: If you dimension a dynamic array and you then use an ERASE statement to erase the array, you will also get the &quot;Subscript Out of Range&quot; error when attempting to reference an element of that array. The ERASE statement either reinitializes the elements of a static array or releases the memory used by a dynamic array.

Example of CLEAR Causing &quot;Subscript out of Range&quot;
The following code block produces the error &quot;Subscript out of range&quot; on the &quot;x(11) = 34&quot; assign statement: COMMON x DIM x(30) CLEAR, , 4000 x(11) = 34

Suggested Solution #1
Create a dynamic variable and redimension the variable. ' $DYNAMIC COMMON x DIM x(30) CLEAR, , 4000 REDIM x(30) x(11) = 34

Suggested Solution #2
In QuickBasic version 4.0 or later, or Basic compiler version 6.0 or later, use the LINK /ST[ACK]:n option, where n is the amount of bytes to allocate to the stack space out of DGROUP (maximum allowable stack is somewhere less than 64K).

For related articles, query on the following words in the Microsoft Knowledge Base:

LINK and STACK and default and size and 3.00 and 4.00 and QuickBasic

Additional query words: QuickBas BasicCom 2.00 3.00 4.00 4.00b 4.50 6.00 6.00b 7.00 7.10

Keywords: KB67373

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.