var A: array of Integer; has no fixed size. Allocate it with SetLength(A, 10). Indexes run from 0 to High(A).
Pascal Easy · Lesson 1 · On the house
Dynamic arrays
array of T, SetLength, Length/High, Copy, Insert/Delete, and reference semantics. Lesson 1 is free — on the house.
Lessons · Pascal Easy · Lesson 1 of 10 · On the house
Dynamic arrays
array of T, SetLength, Length/High, Copy, Insert/Delete, and reference semantics.

Dynamic arrays are reference-counted. B := A shares the data, so a write through one shows up in the other until one is resized. Use B := Copy(A) for an independent copy.
Free Pascal 3.2 adds Insert(Value, A, Index), Delete(A, Index, Count), Concat, and array literals such as A := [1, 2, 3].
Growing one element at a time (SetLength(A, Length(A) + 1)) is O(n) per call, because each resize can copy the whole array. Grow the capacity in big steps, such as doubling it, or use TList.
Arrays of arrays (array of array of Integer) can be jagged.
dynamic arrays
program DynArrays;
{$mode objfpc}{$H+}
var
A, B, C: array of Integer;
Jag: array of array of Integer;
I: Integer;
begin
A := [5, 3, 8];
SetLength(A, 5);
A[3] := 1; A[4] := 9;
writeln(Length(A), ' ', High(A));
B := A;
C := Copy(A);
A[0] := 100;
writeln(B[0], ' ', C[0]);
Insert(42, C, 1);
Delete(C, 0, 1);
for I in C do write(I, ' ');
writeln;
SetLength(Jag, 3);
for I := 0 to 2 do SetLength(Jag[I], I + 1);
writeln(Length(Jag[0]), Length(Jag[2]));
end.
Quiz
What does B := A do for dynamic arrays?
Quiz
How do you get an independent copy?
Quiz
What is the first index of a dynamic array?
Quiz
What prints for B[0] and C[0]?
Check
Match the still for: Dynamic arrays.
Open-book: the answers are on this page. Pass every quiz and check (5) to mark the lesson done. This visit: 0/5.