Intel® Fortran Compiler Classic and Intel® Fortran Compiler Developer Guide and Reference

ID 767251
Date 3/22/2024
Public
Document Table of Contents

ALLOCATABLE

Statement and Attribute: Specifies that an object is allocatable. The shape of an allocatable array is determined when an ALLOCATE statement is executed, dynamically allocating space for the array. A character object may have a deferred length that is determined when the object is allocated with an ALLOCATE statement. A allocatable scalar object of any type may be allocated with an ALLOCATE statement.

The ALLOCATABLE attribute can be specified in a type declaration statement or an ALLOCATABLE statement, and takes one of the following forms:

Type Declaration Statement:

type, [att-ls,] ALLOCATABLE [, att-ls] :: a[(d-spec)] [[coarray-spec]][, a[(d-spec)] [[coarray-spec]]...

Statement:

ALLOCATABLE [::] a[(d-spec)] [[coarray-spec]][, a[(d-spec)] [[coarray-spec]]...

type

Is a data type specifier.

att-ls

Is an optional list of attribute specifiers.

a

Is the name of the allocatable object.

d-spec

Is a deferred-shape specification (: [, :] ...), where each colon represents a dimension of the array or a deferred-coshape specification.

coarray-spec

Is a deferred-coshape specification. The left bracket and right bracket are required.

Description

A character object declaration uses LEN=: to indicate it is deferred length.

If the array is given the DIMENSION attribute elsewhere in the program, it must be declared as a deferred-shape array.

When the allocatable object is no longer needed, it can be deallocated by execution of a DEALLOCATE statement.

An allocatable object cannot be specified in a COMMON, EQUIVALENCE, DATA, or NAMELIST statement.

Allocatable objects are not saved by default. If you want to retain the values of an allocatable object across procedure calls, you must specify the SAVE attribute for the object.

Example

! Method for declaring and allocating objects.

INTEGER, ALLOCATABLE :: matrix(:,:)        ! deferred shape
REAL, ALLOCATABLE    :: vector(:)
CHARACTER, ALLOCATABLE :: c1               ! char scalar, len=1
INTEGER, ALLOCATABLE :: k                  ! numeric scalar
CHARACTER(LEN=:), ALLOCATABLE :: c2, c3(:) ! deferred length, shape

ALLOCATE(matrix(3,5),vector(-2:N+2))       ! specifies shapes
ALLOCATE(c1)                               ! specifies scalar, len=1
ALLOCATE(k)                                ! specifies scalar
ALLOCATE(character(len=5) :: c2)           ! specifies scalar, len=5
ALLOCATE(character(len=3) :: c3(2:4))      ! specifies length, shape

The following example shows a type declaration statement specifying the ALLOCATABLE attribute:

REAL, ALLOCATABLE :: Z(:, :, :)

The following is an example of the ALLOCATABLE statement:

REAL A, B(:)
ALLOCATABLE :: A(:,:), B