FutureBasic Logo

<<    Index    >> FutureBasic 5

dynamic   statement

[dim] dynamic arrayName(maxSub1[,maxSub2 ...]) [as dataType]

The dynamic syntax is an alternate version of dim that allows for arrays that can grow as needed. The constant expression used in the parenthesis is ignored. Dynamic arrays may only be created and used as global arrays. Do not attempt to dimension them inside of a local fn.

// 1000 elements max
dynamic myIntArray(1000)

// 2 gig elements max
dynamic hugeEmployeeRecAry(_maxLong) as employeeRec

// 32,767 elements max
dynamic arrayOfRects(_maxInt) as Rect

The maxSub1 , maxSub2 etc. values must be positive static integer expressions. However, since dynamic does not actually allocate any memory, the declared subscripts are used somewhat differently than in a dim statement. The second and subsequent subscripts (if any) determine the internal structure of the array, and space for them will be fully allocated for each element dynamically referenced in the first subscript. But the value of the first subscript (maxSub1) is ignored, and may be arbitrarily set to any value greater than zero. You can actually reference array elements greater than maxSub1, so long as adequate RAM is available to allocate the memory required.

Auto Grow
To obtain the next index for a dynamic array:

begin globals
dynamic MyDynArray(1) as long
end globals
dim nextIndex as long
MyDynArray(567) = 1
nextIndex = fn DynamicNextElement( dynamic( MyDynArray ) )
// nextIndex is 568

Dynamic arrays may only be global in nature and may not be dimensioned inside of a local fn.

Dynamic arrays may not be used to dimension an array of handles.

See Also:
fn DynamicNextElement; kill dynamic; read dynamic; write dynamic