Skip to content
Tutorials Center
My learning

Topics

Lesson 16 of 36 ยท Strong Typing and Generics

Access Types, Dynamic Memory and Linked Lists in Ada

Learn Ada pointers (access types): allocating with new, null, dereferencing, storage pools, Unchecked_Deallocation, linked structures, access to subprograms and safer alternatives like containers.

Ada's pointers are called access types. They exist because linked structures and runtime-sized objects sometimes need them, but the language steers you away from them: most Ada programs use arrays, records and the container library instead. When you do need pointers, Ada's rules make them safer than C's.

Ada code is read-only on this site; C stand-ins mirror the allocation patterns.

Declaring and using access types

ADA
type Integer_Access is access Integer;

P : Integer_Access := new Integer'(42);
Q : Integer_Access;               --  null by default

Put_Line (Integer'Image (P.all));     --  .all dereferences
P.all := P.all + 1;
  • new Integer'(42) allocates an object and initializes it.
  • P.all is the designated object; for records you can also write P.Field (implicit dereference).
  • Every access variable starts as null. Dereferencing null raises Constraint_Error.
  • Access types are typed: an Integer_Access cannot be assigned to a Float_Access, and there is no pointer arithmetic.

Deallocation

Ada does not collect garbage by default. You free memory with a generic procedure you instantiate:

ADA
with Ada.Unchecked_Deallocation;

procedure Free is new Ada.Unchecked_Deallocation (Integer, Integer_Access);

...
Free (P);         --  frees the object and sets P to null

The word "Unchecked" is a warning: if another pointer still refers to the object, it dangles. Because access values always know their type and Free nulls the pointer you give it, many classic errors disappear, but double aliasing can still bite. Ada 2012 added storage pools, which let you control where new gets its memory (a fixed arena, a stack-like pool).

A linked list

ADA
type Node;
type Node_Ptr is access Node;

type Node is record
   Value : Integer;
   Next  : Node_Ptr := null;
end record;

procedure Push_Front (Head : in out Node_Ptr; Value : Integer) is
begin
   Head := new Node'(Value => Value, Next => Head);
end Push_Front;

procedure Print (Head : Node_Ptr) is
   Cur : Node_Ptr := Head;
begin
   while Cur /= null loop
      Put_Line (Integer'Image (Cur.Value));
      Cur := Cur.Next;
   end loop;
end Print;

type Node; is an incomplete declaration, allowing Node_Ptr to be declared before Node is complete.

flowchart LR
    H["Head"] --> A["30 | next"]
    A --> B["20 | next"]
    B --> C["10 | null"]

Push_Front puts each new node before the old head, so the list prints in reverse order of insertion.

Access to objects and subprograms

You can point at existing objects with aliased and 'Access:

ADA
X : aliased Integer := 5;
P : access Integer := X'Access;      --  anonymous access type

The compiler checks accessibility levels: a pointer cannot outlive the object it points to, so 'Access of a local variable cannot escape the procedure. That rule removes the "return pointer to a stack variable" bug entirely. Access-to-subprogram types hold callbacks:

ADA
type Operation is access function (A, B : Integer) return Integer;
Op : Operation := Max'Access;

Prefer containers

Instead of hand-written lists, Ada 2012's standard containers (Ada.Containers.Vectors, Doubly_Linked_Lists, Hashed_Maps, Ordered_Sets) free their memory automatically and are checked. Use access types for tree and graph structures that the containers do not cover, or when an object must outlive its creator.

Stand-in: a linked list with explicit free

The C stand-in builds the same list. A free_list function frees each node and nulls the head, as Unchecked_Deallocation does. Running it under Piston prints the list before and after.

C Stand-in: Push_Front, Print and Free

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The second stand-in shows what Ada checks for you: dereferencing a null access value. The helper stands in for the implicit check, so a null access raises Constraint_Error instead of crashing.

C Stand-in: checked dereference of null

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Common mistakes

  • Forgetting to free memory (no garbage collector by default).
  • Keeping a copy of a pointer after Free on another copy.
  • Hand-writing lists and trees when a container would do.
  • Comparing access values for equality when you meant to compare designated objects (P.all = Q.all).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the initial value of an access variable?

    Access variables default to null, and dereferencing null raises Constraint_Error.
  2. 2. How do you dereference an access value P of type access Integer?

    .all denotes the designated object.
  3. 3. What does Ada.Unchecked_Deallocation do?

    You instantiate it for your type, and calling it frees the object and nulls the pointer.
  4. 4. What does the accessibility rule on 'Access prevent?

    The compiler rejects storing the address of a local object in a longer-lived access value.

Summary

  • Access types are typed pointers: new allocates, .all dereferences, null is the default.
  • Free memory with an instantiation of Ada.Unchecked_Deallocation.
  • Accessibility rules and typing block many classic pointer bugs.
  • Containers are the first choice; use access types for custom linked structures.

Next: Generics: Reusable Code with Parameters