Case 2: Apply on Dynamic Array

Unlike static array, a variable reference dynamic array is of pointer type. Thus, apply SizeOf on a dynamic array point always return 4, that is same as SizeOf(Pointer) no matter how much memory allocated to the dynamic array at runtime.

Length as always, return the number of elements in array.

var A: array of byte; // or TBytes begin SetLength(A, 10); end;

SizeOf(A) return 4

Length(A) return 10

var C: array of char; // or TBytes begin SetLength(C, 10); end;

SizeOf(C) return 4

Length(C) return 10

To get the total number of bytes allocated for a dynamic array, use Length(C) * SizeOf(Char) and it return 20.

Case 3: Apply on Open Array

Open array parameters allow arrays of different sizes to be passed to the same procedure or function. To define a routine with an open array parameter, use the syntax array of type (rather than array[X..Y] of type) in the parameter declaration. For example,

declares a procedure called MyProc that takes a integer array of any size.

We may pass either a static or dynamic array to MyProc but not limited to that.

Example 1: Dynamic array

var A: array of integer; begin SetLength(A, 10); MyProc(A); end;

The output is

SizeOf(A)=40

Length(A)=10

Example 2: Static array

var A: array[0..9] of integer; begin MyProc(A); end;

The output is

SizeOf(A)=40

Length(A)=10

Example 3: Static array

begin MyProc([0,1,2,3,4,5,6,7,8,9]); end;

The output is

SizeOf(A)=40

Length(A)=10

Example 4: Integer variable

Instead of an array, you can pass a variable of the open array parameter's base type. It will be treated as an array of length 1.

var i: integer; begin MyProc(i); end;

The output is

SizeOf(A)=4

Length(A)=1

Example 5: Open Array or Dynamic Array

It is easy to confuse about the array syntax. The syntax of open array parameters resembles that of dynamic array types, but they do not mean the same thing. If you declare a type identifier for an array, it will be treated as dynamic array: