Copyright (c) 1994 J. Adams, W. Brainerd, J. Martin, and B. Smith.
All rights reserved.
This file may not be copied without permission of the authors.

2 ALLOCATE and DEALLOCATE Statements

The ALLOCATE statement creates space for allocatable arrays and variables with the POINTER attribute. The DEALLOCATE statement frees space previously allocated for allocatable arrays and pointer targets. These statements give the user the ability to manage space dynamically at execution time.

Examples:

COMPLEX, POINTER :: HERMITIAN (:, :)   ! Complex array pointer
READ *, M, N
ALLOCATE ( HERMITIAN (M, N) ) 
   . . .
DEALLOCATE (HERMITIAN, STAT = IERR7)

REAL, ALLOCATABLE :: INTENSITIES (:,:) ! Rank-2 allocatable array
DO
   ALLOCATE (INTENSITIES (I, J), &     ! IERR4 will be positive
             STAT = IERR4)             !   if there is
   IF (IERR4 == 0) EXIT                !   insufficient space.
   I = I/2; J = J/2
END DO
   . . .
IF (ALLOCATED (INTENSITIES)) DEALLOCATE (INTENSITIES)

TYPE NODE
   REAL VAL
   TYPE(NODE), POINTER :: LEFT, RIGHT  ! Pointer components
END TYPE NODE
TYPE(NODE) TOP, BOTTOM
   . . .
ALLOCATE (TOP % LEFT, TOP % RIGHT)
IF (ASSOCIATED (BOTTOM % RIGHT)) DEALLOCATE (BOTTOM % RIGHT)

CHARACTER, POINTER :: PARA(:), KEY(:) ! Pointers to char arrays
ALLOCATE (PARA (1000) )
   . . .
KEY => PARA (K : K + LGTH)

Related Topics:

ALLOCATABLE Attribute and Statement
Dynamic Objects
NULLIFY Statement
POINTER Attribute and Statement
Pointers

Related Intrinsics:

ALLOCATED
ASSOCIATED

To Read More About It:

ISO 1539 : 1991, Fortran Standard, 6.3.1, 6.3.3
Fortran 90 Handbook, 6.5.1, 6.5.3
Programmer's Guide to Fortran 90, 4.1.5, 8.1.3


An ALLOCATE statement is:

ALLOCATE ( allocation-list [ , STAT = scalar-integer-variable ] )

An allocation is:

allocate-object [ ( allocate-shape-spec-list ) ]

An allocate object is one of:

variable-name
structure-component

An allocate shape specification is:

[ lower-bound : ] upper-bound

A DEALLOCATE statement is:

DEALLOCATE ( allocate-object-list [ , STAT = scalar-integer-variable ] )

Things to Know:

  1. Each allocate object must be an allocatable array or a pointer; the bounds in the shape specification must be scalar integer expressions.
  2. The status variable (the variable following STAT=) is set to a positive value if an error is detected and is set to zero otherwise. If there is no status variable, the occurrence of an error causes the program to terminate.
  3. For allocatable arrays, an error occurs when there is an attempt 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.
  4. It is not an error to allocate an associated pointer. Its old target connection is replaced by a connection to the newly allocated space. If the previous target was allocated and no other pointer became associated with it, the space is no longer accessible. A pointer may be assigned to point to a portion of an allocated object such as a section of an array. It is not permitted to deallocate such a pointer; only whole allocated objects may be deallocated. It is also not permitted to deallocate a pointer associated with an allocatable array; the allocatable array must be deallocated instead. The ASSOCIATED intrinsic function may be used to determine whether a pointer is associated or if it is associated with a particular target or the same target as another pointer.
  5. When a pointer is deallocated, its association status is set to disassociated (as if a NULLIFY statement were also executed). When a pointer is deallocated, the association status of any other pointer associated with the same (or part of the same) target becomes undefined.