Dynamic Arrays

Dynamic arrays do not have a fixed size or length. Instead, memory for a dynamic array is reallocated when you assign a value to the array or pass it to the SetLength procedure. Dynamic-array types are denoted by constructions of the form array of baseType

For example, var MyFlexibleArray: array of Real;

declares a one-dimensional dynamic array of reals. The declaration does not allocate memory for MyFlexibleArray. To create the array in memory, call SetLength. For example, given the previous declaration,

SetLength(MyFlexibleArray, 20);

allocates an array of 20 reals, indexed 0 to 19. Dynamic arrays are always integer-indexed, always starting from 0.

Dynamic-array variables are implicitly pointers and are managed by the same reference-counting technique used for long strings. To deallocate a dynamic array, assign nil to a variable that references the array or pass the variable to Finalize; either of these methods disposes of the array, provided there are no other references to it. Dynamic arrays are automatically released when their reference-count drops to zero. Dynamic arrays of length 0 have the value nil. Do not apply the dereference operator (A) to a dynamic-array variable or pass it to the New or Dispose procedure.

If X and y are variables of the same dynamic-array type, X := y points X to the same array as y. (There is no need to allocate memory for X before performing this operation.) Unlike strings and static arrays, copy-on-write is not employed for dynamic arrays, so they are not automatically copied before they are written to. For example, after this code executes, var

A, B: array of Integer; begin setLength(A, 1); A[0] := 1; B := A; B[0] := 2; end;

the value of a[0] is 2. (If a and b were static arrays, a[0] would still be 1.)

Assigning to a dynamic-array index (for example, MyFlexibleArray[2] := 7) does not reallocate the array. Out-of-range indexes are not reported at compile time.

In contrast, to make an independent copy of a dynamic array, you must use the global Copy function:

A, B: array of Integer; begin setLength(A, 1); A[0] := 1; B := Copy(A);

When dynamic-array variables are compared, their references are compared, not their array values. Thus, after execution of the code var

A, B: array of Integer; begin

SetLength(A, 1); SetLength(B, 1); A[0] := 2; B[0] := 2; end;

To truncate a dynamic array, pass it to SetLength, or pass it to Copy and assign the result back to the array variable. (The SetLength procedure is usually faster.) For example, if a is a dynamic array, a : = SetLength(A, 0, 20) truncates all but the first 20 elements of a.

Once a dynamic array has been allocated, you can pass it to the standard functions Length, High, and Low. Length returns the number of elements in the array, High returns the array's highest index (that is, Length - 1), and Low returns 0. In the case of a zero-length array, High returns 1 (with the anomalous consequence that High < Low).

Note: In some function and procedure declarations, array parameters are represented as array of baseType, without any index types specified. For example,function CheckStrings(A: array of string): Boolean;

This indicates that the function operates on all arrays of the specified base type, regardless of their size, how they are indexed, or whether they are allocated statically or dynamically.

Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook

Post a comment