ZetCode

Pascal dynamic arrays

last modified November 18, 2026 Free Pascal 3.2.2

A dynamic array has no length in its type. The length is chosen at run time with SetLength, and the array can grow, shrink, or be released while the program runs. Dynamic arrays are managed by the compiler: they are reference counted, they are freed automatically when the last reference goes away, and they can be returned from functions. This article shows how they are declared, resized, copied, and passed around.

Declaring and sizing

A dynamic array is declared as array of T without an index range. Before it can be used, memory has to be reserved with SetLength. The valid indices are then 0 to Length - 1, whatever the declaration looks like.

basics.pas
program basics;
{$mode objfpc}{$H+}

var
  values: array of Integer;
  i: Integer;

begin
  SetLength(values, 5);
  for i := 0 to High(values) do
    values[i] := (i + 1) * (i + 1);
  WriteLn('Length = ', Length(values));
  WriteLn('Low = ', Low(values), ', High = ', High(values));
  for i := Low(values) to High(values) do
  begin
    if i > Low(values) then
      Write(' ');
    Write(values[i]);
  end;
  WriteLn;
end.

After SetLength(values, 5) the array has five elements, indexed 0 to 4. New elements are zeroed, so the first assignment is not strictly necessary, but relying on that would hide the fact that the values are being computed. Low is always 0 for a dynamic array, and High is Length - 1; when the length is zero, High is -1.

$ fpc -gl basics.pas
$ ./basics
Length = 5
Low = 0, High = 4
1 4 9 16 25

Growing an array

SetLength can be called again with a larger or smaller size. The elements that already exist keep their values; new elements are zeroed. The next program appends five values one at a time by increasing the length by one before each assignment.

growing.pas
program growing;
{$mode objfpc}{$H+}

var
  values: array of Integer;
  i: Integer;

begin
  SetLength(values, 0);
  for i := 1 to 5 do
  begin
    SetLength(values, Length(values) + 1);
    values[High(values)] := i * i;
  end;
  for i := Low(values) to High(values) do
  begin
    if i > Low(values) then
      Write(' ');
    Write(values[i]);
  end;
  WriteLn;
  WriteLn('Length = ', Length(values));
end.

Each SetLength may reallocate the block and copy the old elements to the new one, so appending one element at a time is quadratic in the final length. For a large number of elements it is better to reserve the final size once, or to grow in chunks such as 16, 32, 64, and so on. The string processing article uses the same pattern for building strings.

$ fpc -gl growing.pas
$ ./growing
1 4 9 16 25
Length = 5

Slicing with Copy

Copy returns a new dynamic array that contains a range of elements. Its arguments are the source array, the starting index, and the number of elements. The source is not modified, and the result owns its own storage.

copyarr.pas
program copyarr;
{$mode objfpc}{$H+}

var
  a, b: array of Integer;
  i: Integer;

begin
  a := [10, 20, 30, 40, 50];
  b := Copy(a, 1, 3);
  WriteLn('Length(b) = ', Length(b));
  for i := Low(b) to High(b) do
  begin
    if i > Low(b) then
      Write(' ');
    Write(b[i]);
  end;
  WriteLn;
end.

The slice starts at index 1 and takes three elements, so b holds 20, 30, and 40. An array constructor such as [10, 20, 30, 40, 50] is allowed in an assignment to a dynamic array. If the requested range reaches past the end, Copy returns only the elements that exist instead of raising an error.

$ fpc -gl copyarr.pas
$ ./copyarr
Length(b) = 3
20 30 40

References and copy-on-write

A dynamic array variable is a reference to a heap block, not the block itself. Assigning one array to another makes both variables point to the same data. When one of them is resized, the compiler copies the block first, so the other variable keeps the old contents. This is called copy-on-write.

reference.pas
program reference;
{$mode objfpc}{$H+}

var
  a, b: array of Integer;

begin
  a := [1, 2, 3];
  b := a;
  b[0] := 99;
  WriteLn('a[0] = ', a[0]);
  WriteLn('b[0] = ', b[0]);
  SetLength(b, 5);
  b[0] := 7;
  WriteLn('after SetLength, a[0] = ', a[0]);
  WriteLn('after SetLength, b[0] = ', b[0]);
end.

The assignment b[0] := 99 changes the shared block, so both variables see 99. The later SetLength(b, 5) gives b its own block, and the assignment b[0] := 7 no longer affects a. The same rule applies to dynamic arrays stored in records, which the records article returns to.

$ fpc -gl reference.pas
$ ./reference
a[0] = 99
b[0] = 99
after SetLength, a[0] = 99
after SetLength, b[0] = 7

Nil and empty arrays

A dynamic array that has never been sized is nil. Its length is zero, and it can be compared with nil or with an empty array. SetLength(a, 0) releases the storage and leaves the variable in the same empty state.

nilcase.pas
program nilcase;
{$mode objfpc}{$H+}

var
  a: array of Integer;

begin
  WriteLn('Length(a) = ', Length(a));
  if a = nil then
    WriteLn('a is nil');
  SetLength(a, 3);
  WriteLn('Length(a) = ', Length(a));
  SetLength(a, 0);
  WriteLn('Length(a) = ', Length(a));
end.

Length(nil) is 0, so the first line is safe even though no memory has been allocated. After SetLength(a, 0) the variable is empty again and the block is freed when the last reference disappears. A dynamic array never has to be freed with FreeMem; the compiler inserts the release code.

$ fpc -gl nilcase.pas
$ ./nilcase
Length(a) = 0
a is nil
Length(a) = 3
Length(a) = 0

Multidimensional dynamic arrays

A dynamic array can have several dimensions, and each dimension is sized separately. The declaration array of array of Integer describes a jagged array: the rows may have different lengths, even though SetLength(m, 3, 4) makes them equal in the common case.

matrix.pas
program matrix;
{$mode objfpc}{$H+}

type
  TMatrix = array of array of Integer;

var
  m: TMatrix;
  i, j: Integer;

begin
  SetLength(m, 3, 4);
  for i := 0 to High(m) do
    for j := 0 to High(m[i]) do
      m[i, j] := i * 10 + j;
  for i := 0 to High(m) do
  begin
    for j := 0 to High(m[i]) do
      Write(m[i, j]:4);
    WriteLn;
  end;
  WriteLn('rows = ', Length(m), ', columns = ', Length(m[0]));
end.

The two-argument form of SetLength sizes the outer array and every inner array at once. After that, the rows can still be resized independently: SetLength(m[1], 2) would shorten the second row. A jagged array is useful for triangular data and for reading rows of different lengths from a file.

$ fpc -gl matrix.pas
$ ./matrix
   0   1   2   3
  10  11  12  13
  20  21  22  23
rows = 3, columns = 4

Returning arrays from functions

A function can return a dynamic array. The function result is a managed reference, so the array survives the return and is freed when the caller's variable goes out of scope. An open array parameter accepts a dynamic array of any length.

functions.pas
program functions;
{$mode objfpc}{$H+}

type
  TIntArray = array of Integer;

function Range(n: Integer): TIntArray;
var
  i: Integer;
begin
  SetLength(Result, n);
  for i := 0 to n - 1 do
    Result[i] := i * i;
end;

function Sum(const values: array of Integer): Integer;
var
  i: Integer;
begin
  Result := 0;
  for i := Low(values) to High(values) do
    Result := Result + values[i];
end;

var
  squares: TIntArray;

begin
  squares := Range(6);
  WriteLn('Length = ', Length(squares));
  WriteLn('Sum = ', Sum(squares));
  WriteLn('Sum of literal = ', Sum([2, 4, 6, 8]));
end.

Range(6) returns an array of six squares, from 0 to 25, whose sum is 55. Sum accepts both the dynamic array and the array constructor in square brackets. The type name TIntArray makes the return type and the variable declaration easier to read, and it is the usual Pascal convention for a named array type.

$ fpc -gl functions.pas
$ ./functions
Length = 6
Sum = 55
Sum of literal = 20

Next steps

The strings article shows the string types, which are managed in a similar way, and the string processing article builds strings and string lists. The records article explains what happens when a dynamic array is a field of a record, and the pointers article contrasts managed arrays with manual memory allocation.

Source

Free Pascal Reference guide

Free Pascal: dynamic arrays

Pascal on Wikipedia

In this article we have declared dynamic arrays, sized and resized them with SetLength, sliced them with Copy, observed copy-on-write, handled nil and empty arrays, built multidimensional arrays, and returned arrays from functions. The series continues with Pascal strings.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Pascal tutorials.