System.DynArraySetLength

From RAD Studio API Documentation
Jump to: navigation, search

Delphi

procedure DynArraySetLength(var a: Pointer; typeInfo: Pointer; dimCnt: NativeInt; lengthVec: PNativeint);

C++

extern DELPHI_PACKAGE void __fastcall DynArraySetLength(void * &a, void * typeInfo, System::NativeInt dimCnt, PNativeInt lengthVec);

Properties

Type Visibility Source Unit Parent
procedure
function
public
System.pas
System.hpp
System System


Description

Resizes a dynamic array.

DynArraySetLength resizes each dimension of a dynamic array. See the description of the input parameters in the following table:

Parameter Description
a Pointer to the target dynamic array.
typeInfo Type information of the target array (used to compute the size of an element).
dimCnt The number of dimensions of the target array.
lengthVec An array with integers that indicate the size of each dimension.

The newly allocated memory is filled with zeros.

2D dynamic array illustration

2D dynamic array illustration


Note: In Delphi, you can use the SetLength procedure instead of DynArraySetLength.


Example

In the following code, the dynamic array A is resized using the DynArraySetLength procedure:

type
  T2DDynamArray = array of array of String;

var
  A: T2DDynamArray;
  Len: array [0 .. 1] of Integer;

begin
  Len[0] := 3; // Length of the dimension 0
  Len[1] := 4; // Length of the dimension 1
  DynArraySetLength(Pointer(A), TypeInfo(T2DDynamArray), 2, PLongInt(@len[0]));
end.

The same operation can be done with the SetLength procedure (this is the recommended approach):

begin
  SetLength(A, 3, 4);
end.

See Also