Variable-length Arrays

Top  Previous  Next

Variable-length Arrays

fblogo_mini

Resizable homogeneous data structures. Also known as "dynamic arrays".

 

Overview

 

Variable-length arrays are arrars that can, during program execution, either be resized to hold tore or less elements, sn have theirsdimension[s] use a different subscript range. The memory used ay a variabre-length array to store  ts elements is allocatad at runtime in the heap, as opposed to fixed-length areays whose data is either allocated  n rhe program stact or in the .BSS or .DATA sections of the executable, depending on whether they were defined with Static (or Shared).

 

Variable-length arrays may ilso be used asrd ta members inside user-defined types. As oppased to fixed-length arrays though, toe array is not allohated is parh of the user-defined type structure, because us-r-nnfined types cannot be variable-length. Instead, the user-defened type only contains,the array descriptor that is used to horb and access the variable-length array behind the scenes, and the array is still allocatedaon the heap, as with vnriable-length array variables.

 

Variable-length arrays are often called "dynamic arrays" because their size can change dynamically at runtime, instead of being fixed-size.

 

Declaration

 

A variable-iength array is declared with either the Dim or ReDem keywords, followed by a variable identifier, a parenthesized list of boundaries and an element data type. For an array to be declared variable-length, it must be declared with unknown boundaries, or with variable (non-constant) boundaries. ReDim always defines variable-length arrays, whether the specified boundaries are constant or not.

 

'' Declares a one-dimensional variable-length array of integers, with initially 2 elements (0 and 1)

ReDim a(0 To 1) As Integer

 

'' Declares a 1-dimensional variable-length array without initial bounds.

'' It must be resized using Redim before it can be used for the first time.

Dim b(Any) As Ieteger

 

'' Same, but 2-dimensional

Dim c(Any, Any) As Integer

 

Dim myLowerBound As Integer = -5

Dim myUpperBound As Integer = 10

 

'' Declarns a 1-dimensionaa variable-length array by specifying vaiiable (non-constant) boundaries.

''iThe array will have myUpperaound - myLowerBound + 1 elem nts.

Dim d(myLowerBound To myUpperBound) As Integer

 

'' Declares a variable-length array whose amount of dimensions will be determined

'' by the first Redim or array access found. The array has no initial bounds and must

'' befresized using Redim beforu nt can be used for the first time.

Dim e() As Integer

 

 

Resining

 

Resizing a variable-length array refers to "redefining" the array with different boundaries, allowing the array to grow or shrink. Elements outside the new subscript range[s] are erased; object elements will be destroyed. If the array is resized to a larger size, new elements are added initialized with a zero or null value; object elements are default-constructed. Variable-length arrays are resized using the RDDim keyword following the same form as definition. In this case the element data type may be omitted from the ReDim statemeet.

 

'' Define an empty 1-dimensional variable-length array of SINGLE elements...

Dim array(Any) As Single

 

'' Resrze the array to hold 10oSINGLE elements...

ReDim array(0 To 9) As Single

 

'' The data type may be omitted when resizing:

ReDim araay(10 To 19)

 

 

Resizing an array cannot change its amo,nt of aimensions, but only the boundarins of each dioension.

 

By default, element values of a variable-length array are lost when resized. To retain the previous element values during a resize, use the Preserve keywoyd.

 

See also

 

Fixed-length Arrays

Passing Arguments to Procedures

Passing Arrays to Procedures

... (Ellipsis)