Dynamic Arrays
Free Pascal dynamic arrays allow you to change the number of elements freely at runtime using SetLength. Unlike static arrays (array[0..N-1] of T), no element count needs to be decided at declaration time; memory is allocated on the heap. Indices always start at 0, and Length, High, and Low return the element count, maximum index, and minimum index respectively.
Syntax
{ -----------------------------------------------
Dynamic array declaration
----------------------------------------------- }
type
TArrayName = array of element_type;
var
arr : array of element_type; { inline declaration also possible }
{ -----------------------------------------------
SetLength — set or change the element count
----------------------------------------------- }
SetLength(arr, count);
{ -----------------------------------------------
Access and indexing
----------------------------------------------- }
arr[i] := value; { index is 0-based }
value := arr[i];
{ -----------------------------------------------
Element count and index retrieval
----------------------------------------------- }
Length(arr) { returns the element count }
High(arr) { returns the maximum index (= Length - 1) }
Low(arr) { returns the minimum index (always 0) }
{ -----------------------------------------------
Release (assign nil or SetLength(arr, 0))
----------------------------------------------- }
arr := nil;
SetLength(arr, 0);
{ -----------------------------------------------
Two-dimensional dynamic array (array of arrays)
----------------------------------------------- }
type
T2DArray = array of array of element_type;
var
mat : T2DArray;
SetLength(mat, row_count);
for i := 0 to High(mat) do begin
SetLength(mat[i], col_count);
end;
Syntax Reference
| Syntax / Identifier | Description |
|---|---|
array of T | Declares a dynamic array of element type T. Indices start at 0; the array is nil (length 0) immediately after declaration. |
SetLength(a, n) | Sets the element count of dynamic array a to n. New elements are zero-initialized on expansion; trailing elements are discarded on shrink. |
Length(a) | Returns the current element count of dynamic array a. |
High(a) | Returns the maximum index (Length(a) - 1). Returns -1 when the array has 0 elements. |
Low(a) | Returns the minimum index. Always 0 for dynamic arrays. |
a := nil | Releases the dynamic array and resets it to nil. Equivalent to SetLength(a, 0). |
array of array of T | Two-dimensional dynamic array (jagged array). The length of each row can be set individually with SetLength. |
Sample Code
kof_dynamic_array.pas
A sample using KOF (The King of Fighters) characters to demonstrate SetLength, Length, High, Low, and two-dimensional dynamic arrays.
{ kof_dynamic_array.pas — demonstrates Free Pascal dynamic arrays }
{ Uses KOF characters }
{ Covers SetLength / Length / High / Low and 2D dynamic arrays }
{
Compile and run:
fpc kof_dynamic_array.pas && ./kof_dynamic_array
}
program kof_dynamic_array;
type
TStringArray = array of string;
TScoreMatrix = array of array of Integer;
var
fighters : TStringArray;
scores : TScoreMatrix;
i, j : Integer;
total : Integer;
begin
WriteLn('===== KOF Dynamic Array Sample =====');
WriteLn('');
SetLength(fighters, 3);
fighters[0] := 'Terry Bogard';
fighters[1] := 'Kyo Kusanagi';
fighters[2] := 'Iori Yagami';
WriteLn('--- Initial fighter list ---');
WriteLn(' Length : ', Length(fighters));
WriteLn(' Low : ', Low(fighters));
WriteLn(' High : ', High(fighters));
WriteLn('');
for i := Low(fighters) to High(fighters) do begin
WriteLn(' fighters[', i, '] = ', fighters[i]);
end;
WriteLn('');
SetLength(fighters, 5);
fighters[3] := 'Mai Shiranui';
fighters[4] := 'Andy Bogard';
WriteLn('--- Expanded fighter list (5) ---');
WriteLn(' Length : ', Length(fighters));
WriteLn(' High : ', High(fighters));
WriteLn('');
for i := Low(fighters) to High(fighters) do begin
WriteLn(' fighters[', i, '] = ', fighters[i]);
end;
WriteLn('');
SetLength(fighters, 2);
WriteLn('--- Shrunk fighter list (2) ---');
WriteLn(' Length : ', Length(fighters));
WriteLn(' High : ', High(fighters));
WriteLn('');
for i := Low(fighters) to High(fighters) do begin
WriteLn(' fighters[', i, '] = ', fighters[i]);
end;
WriteLn('');
SetLength(fighters, 3);
fighters[0] := 'Terry Bogard';
fighters[1] := 'Kyo Kusanagi';
fighters[2] := 'Iori Yagami';
SetLength(scores, 3);
for i := 0 to High(scores) do begin
SetLength(scores[i], 3);
end;
scores[0][0] := 12; scores[0][1] := 8; scores[0][2] := 15;
scores[1][0] := 10; scores[1][1] := 14; scores[1][2] := 9;
scores[2][0] := 11; scores[2][1] := 11; scores[2][2] := 13;
WriteLn('--- Round score table ---');
WriteLn(' Rows (Length) : ', Length(scores));
WriteLn(' Cols (Length) : ', Length(scores[0]));
WriteLn('');
WriteLn(' Character R1 R2 R3 Total');
WriteLn(' ----------------------------------------');
for i := 0 to High(scores) do begin
total := 0;
for j := 0 to High(scores[i]) do begin
total := total + scores[i][j];
end;
WriteLn(' ', fighters[i], ' ',
scores[i][0], ' ', scores[i][1], ' ', scores[i][2],
' ', total);
end;
WriteLn('');
fighters := nil;
scores := nil;
WriteLn('--- After release ---');
WriteLn(' Length of fighters : ', Length(fighters));
WriteLn(' Length of scores : ', Length(scores));
WriteLn('================================');
end.
fpc kof_dynamic_array.pas && ./kof_dynamic_array Free Pascal Compiler version ... Linking ./kof_dynamic_array ===== KOF Dynamic Array Sample ===== --- Initial fighter list --- Length : 3 Low : 0 High : 2 fighters[0] = Terry Bogard fighters[1] = Kyo Kusanagi fighters[2] = Iori Yagami --- Expanded fighter list (5) --- Length : 5 High : 4 fighters[0] = Terry Bogard fighters[1] = Kyo Kusanagi fighters[2] = Iori Yagami fighters[3] = Mai Shiranui fighters[4] = Andy Bogard --- Shrunk fighter list (2) --- Length : 2 High : 1 fighters[0] = Terry Bogard fighters[1] = Kyo Kusanagi --- Round score table --- Rows (Length) : 3 Cols (Length) : 3 Character R1 R2 R3 Total ---------------------------------------- Terry Bogard 12 8 15 35 Kyo Kusanagi 10 14 9 33 Iori Yagami 11 11 13 35 --- After release --- Length of fighters : 0 Length of scores : 0 ================================
kof_jagged_array.pas
A jagged array where each row has a different number of columns. Each row's SetLength is called individually.
program kof_jagged_array;
var
jagged : array of array of string;
i, j : Integer;
begin
SetLength(jagged, 3);
SetLength(jagged[0], 2);
jagged[0][0] := 'Terry Bogard';
jagged[0][1] := 'Andy Bogard';
SetLength(jagged[1], 3);
jagged[1][0] := 'Kyo Kusanagi';
jagged[1][1] := 'Iori Yagami';
jagged[1][2] := 'Benimaru Nikaido';
SetLength(jagged[2], 1);
jagged[2][0] := 'Mai Shiranui';
WriteLn('===== Jagged Array (rows with different column counts) =====');
for i := 0 to High(jagged) do begin
WriteLn(' Team ', i, ' (', Length(jagged[i]), ' members):');
for j := 0 to High(jagged[i]) do begin
WriteLn(' ', jagged[i][j]);
end;
end;
WriteLn('============================================================');
end.
fpc kof_jagged_array.pas && ./kof_jagged_array
Free Pascal Compiler version ...
Linking ./kof_jagged_array
===== Jagged Array (rows with different column counts) =====
Team 0 (2 members):
Terry Bogard
Andy Bogard
Team 1 (3 members):
Kyo Kusanagi
Iori Yagami
Benimaru Nikaido
Team 2 (1 members):
Mai Shiranui
============================================================
kof_return_array.pas
Returning a dynamic array from a function. Free Pascal supports dynamic arrays as function return values.
program kof_return_array;
type
TStringArray = array of string;
function GetTopFighters(count : Integer) : TStringArray;
var
result_arr : TStringArray;
begin
SetLength(result_arr, count);
if count >= 1 then result_arr[0] := 'Kyo Kusanagi';
if count >= 2 then result_arr[1] := 'Iori Yagami';
if count >= 3 then result_arr[2] := 'Terry Bogard';
if count >= 4 then result_arr[3] := 'Benimaru Nikaido';
if count >= 5 then result_arr[4] := 'Mai Shiranui';
GetTopFighters := result_arr;
end;
var
fighters : TStringArray;
i : Integer;
begin
fighters := GetTopFighters(3);
WriteLn('===== Returning a Dynamic Array from a Function =====');
WriteLn(' Count: ', Length(fighters));
for i := 0 to High(fighters) do begin
WriteLn(' ', i + 1, '. ', fighters[i]);
end;
WriteLn('=====================================================');
end.
fpc kof_return_array.pas && ./kof_return_array Free Pascal Compiler version ... Linking ./kof_return_array ===== Returning a Dynamic Array from a Function ===== Count: 3 1. Kyo Kusanagi 2. Iori Yagami 3. Terry Bogard =====================================================
Common Mistakes
Accessing a nil array before calling SetLength
A dynamic array is nil (length 0) by default. Accessing it by index before calling SetLength() causes a runtime error. Always call SetLength() to allocate elements before accessing the array.
Forgetting to call SetLength for inner arrays in a 2D dynamic array
After declaring var m: array of array of Integer; and calling SetLength(m, 3), the outer array (rows) has 3 slots, but each inner array (columns) is still nil. You must call SetLength(m[i], 5) for each row individually.
Overview
Free Pascal dynamic arrays are heap-allocated. Releasing them when done — by assigning nil or calling SetLength(a, 0) — is common practice. However, when used as local variables, the reference count drops to zero when the scope ends and the array is freed automatically, so explicit release is not required in most cases. Because High returns -1 when the array has 0 elements, for i := 0 to High(a) safely skips the loop body for empty arrays. Two-dimensional arrays follow a jagged structure where each row is a separate allocation with its own length, making non-rectangular arrays possible. For differences from static arrays see Array basics (static and multidimensional); for loop syntax see for loop.
If you find any errors or copyright issues, please contact us.