Microsoft KB Archive/32416

= Microsoft Knowledge Base =

Using Static/Dynamic Arrays in Compiler and Interpreter
Last reviewed: January 12, 1995

Article ID: Q32416

Summary:

All arrays in the Macintosh BASIC interpreter are dynamic, but the compiler permits both static and dynamic arrays. Dynamic arrays can be erased and redimensioned during run time. Dynamic arrays give your program more control over how it uses memory. Static arrays are set up at compile time and cannot be redimensioned during run time.

In compiled programs, static arrays are much more efficient than dynamic arrays because static array indices are calculated at compile time and do not have to be recalculated at run time. Whenever possible, specify static arrays for the compiler.

More Information:

You can make your programs handle arrays correctly in both the compiler and the interpreter in the following three ways:

1. Define all arrays you would like to be compiled as static arrays as DIM STATIC in your program's source code. The QuickBASIC interpreter allows this declaration but ignores it and defines your arrays as dynamic. This assures compatibility with the compiler. 2. If none of the arrays in your program needs to be redimensioned and the size of the arrays is known at compile time, you can choose the &quot;Use Static Arrays&quot; option from the &quot;Options...&quot; command in the Run menu. This option makes all arrays static. Therefore, arrays will be dynamic in the QuickBASIC interpreter, but static when compiled. If you choose the &quot;Use Static Arrays&quot; option, you may not use the ERASE statement because you can only ERASE dynamic arrays. Also, all static array sizes must be     specified with constants or simple constant expressions, not with variables. 3. If some, but not all, arrays in your program must be     redimensioned at run time, or if the size of some arrays is not known at compile time, you must declare static arrays separately for the compiler. Dynamic arrays need no special treatment because they are dynamic in both environments. Arrays that can be static for the compiler must still be dynamic in the interpreter.