HP Fortran Programmer's Reference (September 2007)

HP Fortran statements
ALLOCATE
Chapter 10 277
Description
The ALLOCATE statement creates space for allocatable arrays and targets for variables (scalars
or arrays) with the POINTER attribute. The ALLOCATE and DEALLOCATE statements give the
user the ability to manage space dynamically at execution time.
For allocatable arrays, an error occurs when an attempt is made to allocate an already
allocated array or to deallocate an array that is not allocated. The ALLOCATED intrinsic
function may be used to determine whether an allocatable array is allocated.
A pointer can be associated with a target, either with the pointer assignment statement or by
use of the ALLOCATE statement. It is not an error to allocate an already associated pointer; its
old target connection is replaced by a connection to the newly allocated space. However, if the
previous target was allocated and no other pointer became associated with it, the space is no
longer accessible.
Examples
In the following example, a complex array with the POINTER attribute is declared. Target
space is allocated to it at run-time, the amount being determined by two integer values read
in. Later in the program, the space is recovered by use of the DEALLOCATE statement.
COMPLEX, POINTER :: hermitian (:, :)
READ *, m, n
ALLOCATE (hermitian (m, n))
DEALLOCATE (hermitian, STAT = ierr)
In the next example, a real allocatable array is declared. The amount of space allocated to it
depends on how much is available.
! Rank-2 allocatable array
REAL, ALLOCATABLE :: intense(:,:)
CALL init_i_j(i, j)
DO
ALLOCATE (intense(i, j), STAT = ierr4)
! ierr4 will be positive if there is not enough space to
! allocate this array
IF (ierr4 == 0) EXIT
i = i/2; j = j/2
END DO
The derived type node in the next example is the basis of a binary tree structure. It consists of
a real value component (val) and two pointer components, left and right, both of type node.
The variable top (of type node) is declared, and space is allocated for targets for the pointers
top%left and top%right.