next up previous

4.3 Dynamic Arrays     continued...

Allocatable Arrays
Allocatable arrays are those explicitly declared ALLOCATABLE. An allocatable array may be local to a procedure or may be placed in a module and effectively be global to all procedures of the application. An allocatable array is explicitly allocated with the ALLOCATE statement, and deallocated either explicitly with the DEALLOCATE statement or, if it is a local array for which SAVE has not been specified, automatically upon exit from the procedure. (If SAVE has been specified, local allocatable arrays can persist from one execution of the procedure to the next - they must be explicitly deallocated with a DEALLOCATE statement.) A global allocatable array persists until it is explicitly deallocated, which may occur in a procedure different from the one in which it was allocated. Use an allocatable (or pointer) array if its size depends on a computed value other than a dummy argument or variable in a module, common, or the host. The allocation status (allocated or not allocated) of an allocatable array may be tested with the ALLOCATED intrinsic function. Examples of allocatable arrays are:
subroutine Peach
 use Recipe                    ! Accesses global allocatable array, Jam.

 real, allocable :: Pie(:,:)   ! Pie is a 2-dimensional allocatable array.
 allocate ( Pie(N,2*N )   )    ! Allocate a local allocatable array.

 if (.not.allocated(Jam)) allocate ( Jam(4*M) )
                               ! Allocate a global allocable array if
                               ! it is not already allocated.
 ... deallocate ( Pie )
end subroutine Peach

module Recipe                  ! Jam is a global allocatable array, and
 real, allocable :: Jam(:)     ! can be allocated and deallocated in
 ...                           ! any procedure(s) using this module.
end module Recipe
Note that the declared bounds for allocatable arrays are simply colons, indicating that these will be provided later, at the time of allocation. This makes allocatable array declaration appear similar to assumed-shape dummy argument declaration, appropriate because the ``deferred" nature of the sizes of the dimensions is conceptually similar.