Pascal pointers
last modified November 18, 2026 Free Pascal 3.2.2
A pointer is a value that holds the address of another value. Pascal types the
pointer: ^Integer points to an integer, ^TPerson to a
record, and the compiler checks that the address is used with the right type.
Pointers make it possible to build linked structures, to allocate memory while
a program runs, and to pass large data to C libraries. This article covers the
address operator, dereferencing, heap allocation, pointer arithmetic, pointers
to records, nil checks, and leak detection.
Addresses and dereferencing
The @ operator returns the address of a variable, and the
^ operator after a pointer dereferences it, reading or writing the
value at that address. A pointer type is written with a caret before the target
type: ^Integer is a pointer to an integer. The program below takes
the address of a local variable and changes the variable through the pointer.
program basic;
{$mode objfpc}{$H+}
var
Value: Integer;
P: ^Integer;
begin
Value := 42;
P := @Value;
WriteLn('Value = ', Value);
WriteLn('P^ = ', P^);
WriteLn('address = ', PtrUInt(P));
P^ := 99;
WriteLn('Value = ', Value);
end.
P := @Value stores the address of Value in the
pointer. P^ reads through the address, and the assignment
P^ := 99 writes through it, so the original variable changes.
PtrUInt(P) converts the pointer to an unsigned integer so that the
address can be printed; the exact number depends on where the program was
loaded and is different on every run.
$ fpc -gl basic.pas $ ./basic Value = 42 P^ = 42 address = 4402608 Value = 99
New and Dispose
A pointer can also refer to memory that is allocated on the heap while the
program runs. New(P) reserves a block large enough for the target
type and stores its address in P; Dispose(P) releases
the block. The block is not tied to a variable's scope, so it lives until it is
disposed, even after the procedure that allocated it returns.
program newdispose;
{$mode objfpc}{$H+}
var
P: ^Integer;
Q: PInteger;
begin
New(P);
P^ := 17;
WriteLn('P^ = ', P^);
Dispose(P);
GetMem(Q, SizeOf(Integer));
Q^ := 23;
WriteLn('Q^ = ', Q^);
FreeMem(Q);
end.
PInteger is a predefined alias for ^Integer;
P and Q have the same type. New and
Dispose work with typed pointers, while GetMem and
FreeMem work with a byte count and accept any pointer. The two
pairs must not be mixed: memory from New is released with
Dispose, and memory from GetMem with
FreeMem.
$ fpc -gl newdispose.pas $ ./newdispose P^ = 17 Q^ = 23
Raw memory and pointer arithmetic
GetMem allocates a block of a given size, which may hold an array
whose length is chosen at run time. Arithmetic on a typed pointer is scaled by
the size of its target type, so Data + 1 points to the next
integer, not to the next byte. The elements are reached with
(Data + I)^.
program rawmemory;
{$mode objfpc}{$H+}
var
Data: ^Integer;
I: Integer;
begin
GetMem(Data, 5 * SizeOf(Integer));
for I := 0 to 4 do
(Data + I)^ := I * I;
for I := 0 to 4 do
begin
if I > 0 then
Write(' ');
Write((Data + I)^);
end;
WriteLn;
FreeMem(Data);
end.
The block holds five integers, and the loop writes the squares 0, 1, 4, 9, and
16. Unlike New, GetMem does not initialize the bytes,
so every element must be assigned before it is read. Pointer arithmetic is not
bounds checked, even with range checking enabled: going past the block reads
whatever follows it in memory.
$ fpc -gl rawmemory.pas $ ./rawmemory 0 1 4 9 16
Pointers to records
A pointer to a record is the basis of every linked structure. The record holds
the data and one or more pointers to other records of the same type. The fields
are reached with P^.Field; ObjFPC mode does not accept the
C-style arrow P->Field, so the caret is always written.
program recptr;
{$mode objfpc}{$H+}
type
PPerson = ^TPerson;
TPerson = record
Name: string;
Age: Integer;
end;
var
P: PPerson;
begin
New(P);
P^.Name := 'Alice';
P^.Age := 30;
WriteLn(P^.Name, ', ', P^.Age);
Dispose(P);
end.
The pointer type must be declared before the record that contains it, which is
why PPerson appears above TPerson. The record holds a
managed string; Dispose runs the hidden finalization code for that
field, so the string is released as well. For a record with several pointer
fields, as in a tree node, the program must dispose the child nodes first and
the parent last.
$ fpc -gl recptr.pas $ ./recptr Alice, 30
Nil pointers
A pointer that does not refer to anything has the value nil.
Dereferencing nil is a runtime error, so a pointer that may be
absent should be tested before it is used. nil is also the value
that terminates a linked list: the last node points to nothing.
program nilptr;
{$mode objfpc}{$H+}
var
P: ^Integer;
begin
P := nil;
WriteLn('P = nil: ', P = nil);
if P <> nil then
WriteLn(P^)
else
WriteLn('nothing to read');
end.
Local pointer variables are not automatically initialized to nil;
this program assigns it explicitly. In a record, a pointer field that has never
been set may contain garbage, so a node constructor should set its links to
nil. Comparing a pointer with nil is allowed, but
comparing two unrelated pointers for ordering is not meaningful.
$ fpc -gl nilptr.pas $ ./nilptr P = nil: TRUE nothing to read
Finding leaks with heaptrc
Every New or GetMem must be matched by a
Dispose or FreeMem. A block that is never released is
a memory leak, and a pointer that is used after its block was released is a
dangling pointer. The compiler can link the heaptrc unit into the
program with fpc -gh; when the program ends, heaptrc prints a
summary of allocated and freed blocks and a call trace for each block that is
still allocated.
The trace is written to standard error, after the program's own output. A
report with 0 unfreed memory blocks means that every allocation
was released. The trace makes the location of the allocation visible, which is
usually enough to find the missing Dispose. Compile with both
-gl and -gh to get line numbers in the trace.
When pointers are the right tool
Most Pascal code does not need explicit pointers. Classes are already
references, dynamic arrays and strings are managed, and var
parameters let a procedure modify its caller's variable. Pointers are the right
choice for linked lists, trees, and other structures whose shape changes at run
time, for interfacing with C libraries that expect addresses, and for the rare
case where a data structure must outlive the scope that created it.
Managed types such as strings and dynamic arrays should not be stored in raw
memory without care: GetMem does not initialize them, and
FreeMem does not finalize them. Use New and
Dispose for a single managed value, or keep managed fields in a
record that is allocated with New. The
linked lists article puts these rules to
work.
Next steps
The linked lists article builds singly and doubly linked lists from node pointers, the binary trees article uses two pointers per node, and a later article shows pointers to code. The dynamic arrays article contrasts managed arrays with the manual allocation shown here.
Source
In this article we have taken addresses with @, dereferenced
pointers with ^, allocated and released heap memory with
New/Dispose and GetMem/FreeMem,
used pointer arithmetic, pointed to records, tested for nil, and
enabled the heaptrc leak report. The series continues with
linked lists in Pascal.
Author
List all Pascal tutorials.