Ada access types
last modified November 23, 2026
An access type is Ada's name for a pointer: its values designate objects that
are created while the program runs, rather than objects declared with a name in
the source. Access types make it possible to build structures whose shape is
decided at run time, such as linked lists and trees, and they also let one part
of a program refer to an object owned by another. This article shows allocation
with new, dereferencing with .all, the special value
null, access to existing variables through 'Access,
explicit deallocation, and a complete linked list.
A first access type
The declaration type Int_Ptr is access Integer; defines a type
whose values point at integers. A new integer object is created with
new, and the expression after it gives the initial value. The
suffix .all denotes the designated object itself, so
P.all can be read and assigned like an ordinary integer.
with Ada.Text_IO;
use Ada.Text_IO;
procedure Pointer_Demo is
type Int_Ptr is access Integer;
P : constant Int_Ptr := new Integer'(42);
begin
Put_Line ("value =" & Integer'Image (P.all));
P.all := 10;
Put_Line ("value =" & Integer'Image (P.all));
end Pointer_Demo;
P is a constant because the pointer itself is never changed: it
always designates the same integer object. The object is not constant, so
P.all := 10 is allowed and the second line prints the new value.
This distinction between a constant pointer and the value it designates is
important, because a constant access value can still give write access to its
target.
$ gnatmake -gnat2022 -gnatwa pointer_demo.adb $ ./pointer_demo value = 42 value = 10
Null access values
Every access type has the value null, which designates no object.
An access object that is declared without an initial value is null, and a
program tests it with = or /= before dereferencing it.
The following program passes both a null pointer and a real one to a procedure
that performs the test.
with Ada.Text_IO;
use Ada.Text_IO;
procedure Null_Pointer is
type Int_Ptr is access Integer;
procedure Show (P : Int_Ptr) is
begin
if P = null then
Put_Line ("pointer is null");
else
Put_Line ("value =" & Integer'Image (P.all));
end if;
end Show;
begin
Show (null);
Show (new Integer'(7));
end Null_Pointer;
The else branch is the only place where P.all is
evaluated, so the program never dereferences a null value. Doing so raises
Constraint_Error at run time. A null check is therefore the
access-type equivalent of testing whether an optional object is present.
$ gnatmake -gnat2022 -gnatwa null_pointer.adb $ ./null_pointer pointer is null value = 7
Access to records
An access type may designate a record, which is how linked structures are
built. Ada dereferences automatically in a selected component, so
P.X means P.all.X. The type below is declared
not null access Point, which removes null from its
set of values and documents that a pointer of this type must always designate a
point.
with Ada.Text_IO;
use Ada.Text_IO;
procedure Record_Pointer is
type Point is record
X : Integer;
Y : Integer;
end record;
type Point_Ptr is not null access Point;
P : constant Point_Ptr := new Point'(X => 3, Y => 4);
begin
Put_Line ("x =" & Integer'Image (P.X));
Put_Line ("y =" & Integer'Image (P.Y));
P.all := (X => 10, Y => 20);
Put_Line ("x =" & Integer'Image (P.X));
end Record_Pointer;
The aggregate after new initializes both components, and the whole
record is replaced later through P.all. A not null
access type is a promise that the compiler helps to keep, but the help is not
complete: GNAT accepts P := null; with the warnings
"null not allowed in null-excluding objects" and "Constraint_Error will be
raised at run time", and the assignment then raises
CONSTRAINT_ERROR : ... access check failed. The warning is easy to
miss, so null checks remain necessary for ordinary access types.
$ gnatmake -gnat2022 -gnatwa record_pointer.adb $ ./record_pointer x = 3 y = 4 x = 10
Pointing at an existing variable
So far every designated object came from new. An access value can
also point at a variable that already has a name. Two conditions apply: the
variable must be declared aliased, and the access type must be
declared with access all. The attribute 'Access then
produces the pointer.
with Ada.Text_IO;
use Ada.Text_IO;
procedure Existing_Object is
type Int_Ptr is access all Integer;
Value : aliased Integer := 5;
P : constant Int_Ptr := Value'Access;
begin
Put_Line ("value =" & Integer'Image (P.all));
P.all := 9;
Put_Line ("value =" & Integer'Image (Value));
end Existing_Object;
P and Value designate the same storage, so the
assignment through P.all is visible when Value is
printed. No allocation takes place, and nothing should be deallocated through
P: the variable is owned by its declaration, not by the pointer.
This form is common when a subprogram must modify a caller's variable
indirectly, and it is the reason access all exists as a separate,
more permissive kind of access type.
$ gnatmake -gnat2022 -gnatwa existing_object.adb $ ./existing_object value = 5 value = 9
Releasing memory
Objects created with new live until the program ends unless they
are explicitly freed. Ada does not collect garbage, so the program instantiates
Ada.Unchecked_Deallocation for the designated type and calls the
resulting procedure. The name "unchecked" is a warning: after the call the
pointer is set to null, but the program must not keep or use any other copy of
the old value.
with Ada.Text_IO;
with Ada.Unchecked_Deallocation;
use Ada.Text_IO;
procedure Free_Demo is
type Int_Ptr is access Integer;
procedure Free is new Ada.Unchecked_Deallocation (Integer, Int_Ptr);
P : Int_Ptr := new Integer'(100);
begin
Put_Line ("value =" & Integer'Image (P.all));
Free (P);
if P = null then
Put_Line ("memory released");
end if;
end Free_Demo;
The generic takes the designated type and the access type, and its single
parameter is in out, which is why P must be a
variable. After Free (P) the storage is returned to the system and
the pointer is null. Calling Free a second time on the same value is
harmless only because the pointer is already null; freeing a copy of the
pointer twice would corrupt the heap.
$ gnatmake -gnat2022 -gnatwa free_demo.adb $ ./free_demo value = 100 memory released
Nodes that point to nodes
A linked list needs a record with a component of its own access type. Ada
cannot declare that access type inside the record before the record is
complete, so the type Node is declared incomplete first, then the
access type, and only then the full record. The last node stores
null in Next.
with Ada.Text_IO;
use Ada.Text_IO;
procedure Node_Demo is
type Node;
type Node_Ptr is access Node;
type Node is record
Value : Integer;
Next : Node_Ptr;
end record;
First : constant Node_Ptr := new Node'(Value => 10, Next => null);
Second : constant Node_Ptr := new Node'(Value => 20, Next => First);
begin
Put_Line ("second =" & Integer'Image (Second.Value));
Put_Line ("first =" & Integer'Image (Second.Next.Value));
Put_Line ("end of list =" & Boolean'Image (First.Next = null));
end Node_Demo;
Second.Next is an access value, and selecting
Value from it dereferences that value automatically, so
Second.Next.Value is the first node's value. Testing
First.Next = null detects the end of the chain. This is the
smallest useful linked structure; the next section adds procedures that build
and traverse a longer list.
$ gnatmake -gnat2022 -gnatwa node_demo.adb $ ./node_demo second = 20 first = 10 end of list =TRUE
An applied example: a linked list
The full list keeps a Head pointer, inserts new nodes at the
front, and prints the chain with a loop. The node type and the deallocation
procedure are the same as before; only the operations around them are new.
with Ada.Text_IO;
with Ada.Unchecked_Deallocation;
use Ada.Text_IO;
procedure Linked_List is
type Node;
type Node_Ptr is access Node;
type Node is record
Value : Integer;
Next : Node_Ptr;
end record;
procedure Free is new Ada.Unchecked_Deallocation (Node, Node_Ptr);
Head : Node_Ptr := null;
procedure Push (Value : Integer) is
begin
Head := new Node'(Value => Value, Next => Head);
end Push;
procedure Print_All is
P : Node_Ptr := Head;
begin
while P /= null loop
Put (Integer'Image (P.Value));
P := P.Next;
end loop;
New_Line;
end Print_All;
begin
for I in 1 .. 5 loop
Push (I * 10);
end loop;
Print_All;
while Head /= null loop
declare
P : Node_Ptr := Head;
begin
Head := Head.Next;
Free (P);
end;
end loop;
Put_Line ("list is empty");
end Linked_List;
Push inserts at the front, so the values appear in reverse order:
50, 40, 30, 20, 10. Each Integer'Image begins with a space for a
non-negative value, which separates the numbers in the output. The final loop
removes the nodes one by one; it saves Head in P,
advances Head, and only then frees the old first node, because
reading Head.Next after the deallocation would be invalid.
$ gnatmake -gnat2022 -gnatwa linked_list.adb $ ./linked_list 50 40 30 20 10 list is empty
Next steps
The next article covers exceptions, the mechanism that reports a null dereference, an out-of-range index, or a user-defined error and lets a program recover from it. The linked structures built here return in the articles on stacks, queues, and trees, after generics make it possible to write one list package for many element types.
Source
In this article we have declared access types, allocated objects with
new, read and written them through .all, tested and
assigned null, used a not null access type, pointed at
an aliased variable with 'Access, released memory with
Unchecked_Deallocation, and built, traversed, and destroyed a
linked list. The records article explains the
components and aggregates that each node uses.
Author
List all Ada tutorials.