13 Access types


Access types (and the objects they access) are used to create dynamic data structures. It should be noted that access types are not necessarily implemented as pointers are in other languages (e.g. the address of the object in question), although in many cases they are. They can be used to create dynamic data structures such as linked lists, trees, graphs etc.

Access types

Access types allow for the dynamic referencing and allocation of objects in Ada.

To specify an access type consider the following:

To dynamically create an object that someone will point at:

An object referred to by someone can be referenced in exactly the same way as an explicity declared object.


A special value called null is used to indicate that the access object does not reference any valid objects. An access object is always initialised to null.

Given the following declarations

This will output the string "sue ". When the line highlighted with a star is executed, the value of the access object y is assigned to x, not the object that y was accesses. Both x and y both refer to the same object, so a change to the object that y refers to implicity changes the object x refers to.

If we wish to change a field of the object refered to by x

Access objects x and y still point to distinct objects. Both access objects are implicitly dereferenced in this example. Because of the implicit derefence, we need a special syntax to denote the entire object the access object accesses. Ada has the reserved word all for this purpose.

A comparison of Pascal, C and Ada pointer/access syntax.

Ada95. Typically all access objects are allocated from a storage pool (heap) (see below). However it is possible to create an access type to other objects, so long as they are declared aliased, and the access type is declared to point at 'all' objects.

Scope rules ensure that there cannot be any dangling references to objects. The attribute Unchecked_Access can be used to create access values in an unsafe manner.

Access types to subprograms

A feature not available in Ada83, access types to subprograms allow for functional parameterisation such as used in Fortran mathematical libraries, as well as ad-hoc late binding. As for the rest of Ada, this is type safe.

An example from the LRM (3.10).

Self referencing data structures

To define a structure that refers to itself requires normally requires making references to data structures that don't as yet exist. Ada circumvents this problem by allowing for incomplete type declarations. These simply specify the name of a type that is yet to be defined. The compiler expects that the full definition will be given before the end of the source file.

Initialisation of objects

When an object is dynamically allocated its value can be set in the same statement as its creation. This simply uses a qualified (type name specified) aggregate.


The same thing can be done using named aggregates.

Garbage collection

There is no language requirement for deallocated memory to be put back into use for later allocation. You should consult your compiler reference manual for details.

If you want to deallocate memory (similar to free system call in Unix), then you can instantiate the generic procedure Unchecked_Deallocation (child of package Ada in Ada95). It is called unchecked because it does no live object analysis before deallocation.

This would be instantiated as follows...

Free could then be called as follows...

Storage Pools

Ada95 allows a storage pool to be specified from which allocated memory comes from. Different access types can share a pool, typically most user defined access types share the one program wide pool. By extending the abstract type Root_Storage_Pool, defined in packge System.Storage_Pools, users can write their own storage pool type, and then associate it with an access type via the Storage_Pool attribute.

For example (from LRM, 13.11)

Storage pools allow for the possibility access types that are smaller than for a default access type that accesses a default pool.

to the index...