Developer Guide and Reference

ID 767251
Date 10/31/2024
Public
Document Table of Contents

Allocation of Allocatable Arrays

The bounds (and shape) of an allocatable array are determined when it is allocated. Subsequent redefinition or undefinition of any entities in the bound expressions does not affect the array specification.

If the lower bound is greater than the upper bound, that dimension has an extent of zero, and the array has a size of zero. If the lower bound is omitted, it is assumed to be 1.

When an array is allocated, it is definable. If you try to allocate a currently allocated allocatable array, an error occurs.

If an allocatable variable is a coarray, the corank is declared, but the cobounds are determined when it is allocated.

The intrinsic function ALLOCATED can be used to determine whether an allocatable array is currently allocated; for example:

  REAL, ALLOCATABLE :: E(:,:)
  ...
  IF (.NOT. ALLOCATED(E)) ALLOCATE(E(2:4,7))

Allocation Status

During program execution, the allocation status of an allocatable array is one of the following:

  • Not currently allocated

    The array was never allocated or the last operation on it was a deallocation. Such an array must not be referenced or defined.

  • Currently allocated

    The array was allocated by an ALLOCATE statement. Such an array can be referenced after it is defined, or it can be deallocated.

If an allocatable array has the SAVE attribute, it has an initial status of "not currently allocated". If the array is then allocated, its status changes to "currently allocated". It keeps that status until the array is deallocated.

If an allocatable array does not have the SAVE attribute, it has the status of "not currently allocated" at the beginning of each invocation of the procedure where it is declared, or upon entry of a construct where it is declared. If the array's status changes to "currently allocated", it is deallocated if the procedure is terminated by execution of a RETURN or END statement if it is declared in the specification part of a procedure, or upon termination of the construct where it is declared.

Example: Allocating Virtual Memory

The following example shows a program that performs virtual memory allocation. This program uses Fortran standard-conforming statements instead of calling an operating system memory allocation routine.

! Program accepts an integer and displays square root values

  INTEGER(4) :: N
  READ (5,*) N                         ! Reads an integer value
  CALL MAT(N)
  END

! Subroutine MAT uses the typed integer value to display the square
! root values of numbers from 1 to N (the number read)

  SUBROUTINE MAT(N)
  REAL(4), ALLOCATABLE :: SQR(:)       ! Declares SQR as a one-dimensional
                                       !          allocatable array
  ALLOCATE (SQR(N))                    ! Allocates array SQR

  DO J=1,N
     SQR(J) = SQRT(REAL(J))            ! REAL converts integer to default REAL
  ENDDO

  WRITE (6,*) SQR                      ! Displays calculated values
  DEALLOCATE (SQR)                     ! Deallocates array SQR
  END SUBROUTINE MAT