6.1.1 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.

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:

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. 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.

Examples

Example 6-1 shows a program that performs virtual memory allocation. This program uses Fortran 95/90 standard-conforming statements instead of calling an operating system memory allocation routine.

Example 6-1 Allocating Virtual Memory

! 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(FLOATJ(J))          ! FLOATJ converts integer to REAL
  ENDDO

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

For More Information:

For details on the ALLOCATED intrinsic function, see Section 9.3.10.


Previous Page Next Page Table of Contents