HP Fortran Programmer's Reference (September 2007)

Arrays
Array declarations
Chapter 364
Allocatable arrays
An allocatable array is a deferred-shape array with the ALLOCATABLE attribute. Its bounds
and shape are defined when it is allocated with the ALLOCATE statement. Once allocated, the
allocatable array may be used in any context in which any other array may appear. An
allocatable array can also be deallocated with the DEALLOCATE statement.
An allocatable array has an allocation status that can be tested with the ALLOCATED intrinsic
inquiry function. Its status is unallocated when the array is first declared and after it is
deallocated in a DEALLOCATE statement. After the execution of the ALLOCATE statement, its
status is allocated. An allocatable array with the unallocated status may not be referenced
except as an argument to the ALLOCATED intrinsic or in an ALLOCATE statement. If it has the
allocated status, it may not be referenced in the ALLOCATE statement. It is an error to allocate
an allocatable array that is already allocated, or to deallocate an allocatable array either
before it is allocated or after it is deallocated.
In HP Fortran, an allocatable array that is unallocated, is local to a procedure, and does not
have the SAVE attribute. It is automatically deallocated when the procedure exits.
The following example, alloc_array.f90, calls a subroutine that allocates and deallocates an
allocatable array and uses the ALLOCATED intrinsic function to test its allocation status:
Example 3-1 alloc_array.f90
PROGRAM main
! driver program for calling a subroutine that allocates and
! deallocates an allocatable array
CALL test_alloc_array
END PROGRAM main
SUBROUTINE test_alloc_array
! demonstrate how to allocate and deallocate an allocatable array
! the array matrix is rank-2 allocatable array, with no
! shape or storage
REAL, ALLOCATABLE, DIMENSION(:,:) :: matrix
INTEGER :: n
LOGICAL :: sts
! sts is assigned the value .FALSE. as the array is not yet
! allocated
sts = ALLOCATED(matrix)
PRINT *, 'Initial status of matrix: ', sts
PRINT *, 'Enter an integer (rank of array to be allocated):'
READ *,n
! dynamically create the array matrix; after allocation, array