Fixed-length Arrays
 
Fixed-size homogeneous data structures.

Overview

Fixed-length arrays are arrays that have a fixed constant size throughout the execution of a program. The memory used by a fixed-length array to store its elements is allocated at compile-time, either on the stack or in the .BSS or .DATA sections of the executable, depending on whether Static was used to define it. This may allow for quicker program execution since the memory for the array is already allocated, unlike variable-length arrays, whose element memory isn't allocated until runtime.

Fixed-length arrays with automatic storage, have their elements allocated on the program stack, and pointers to these elements remain valid only while the array is in scope. The elements of fixed-length arrays with static storage are allocated in the .DATA or .BSS sections of the executable, depending on whether or not they are initialized when defined, so pointers to these elements remain valid for the entire execution of the program. Fixed-length arrays of any storage class cannot be resized during program execution, only variable-length arrays can.

Fixed-length arrays may also be used as data members inside user-defined types, in which case the array is directly allocated as part of the user-defined type structure.

Declaration

A fixed-length array is declared with either the Dim or Static keywords, followed by a variable identifier, a parenthesized list of boundaries and an element data type.

'' Defines a one-dimensional fixed-length array of type INTEGER having automatic storage.
Dim arrayOfIntegers(69) As Integer

'' Defines a one-dimensional fixed-length array of type SHORT having static storage.
Static arrayOfShorts(420) As Short


There are various ways to specify an array's amount of elements. Each array can have between 1 or 8 dimensions. Each dimension has a lower bound and an upper bound.

Dim a(1) As Integer  '' 1-dimensional, 2 elements (0 and 1)
Dim b(0 To 1) As Integer  '' 1-dimensional, 2 elements (0 and 1)
Dim c(5 To 10) As Integer  '' 1-dimensional, 5 elements (5, 6, 7, 8, 9 and 10)

Dim d(1 To 2, 1 To 2) As Integer  '' 2-dimensional, 4 elements: (1,1), (1,2), (2,1), (2,2)
Dim e(255, 255, 255, 255) As Integer '' 4-dimensional, 256 * 256 * 256 * 256 elements


For an array to be declared fixed-length, the boundaries must be specified using only number literals or Const values or Enum constants.

Const myLowerBound = -5
Const myUpperBound = 10

'' Declares a one-dimensional fixed-length array, holding myUpperBound - myLowerBound + 1 String objects.
Dim arrayOfStrings(myLowerBound To myUpperBound) As String

'' Declares a one-dimensional fixed-length array of bytes,
'' big enough to hold an INTEGER.
Dim arrayOfBytes(0 To SizeOf(Integer) - 1) As Byte