Intel® Fortran Compiler 17.0 Developer Guide and Reference

ALLOCATE

Statement: Dynamically creates storage for allocatable variables and pointer targets.

ALLOCATE ([type::] object[(s-spec)] [, object[(s-spec[,s-spec]...)] ]... [[coarray-spec]]...[, alloc-opt])

type

Is a data type specifier. If specified, the kind type parameters of each object must be the same as the corresponding type parameter values.

object

Is the object to be allocated. It is a variable name or structure component, and must be a pointer or an allocatable object. The object can be of type character with zero length.

s-spec

Is an array shape specification in the form [lower-bound:]upper-bound. Each bound must be a scalar integer expression. The number of shape specifications must be the same as the rank of the object.

coarray-spec

Is a coarray shape specification in the form:

[[lower-bound:] upper-bound, ] ... [lower-bound:] *

The brackets around coarray-spec are required. A coarray shape specification can only appear if the object is a coarray. The number of coarray specifications must be one less than the corank of the coarray object.

alloc-opt

(Output) Is one of the following:

STAT=stat-var

stat-var is a scalar integer variable in which the status of the allocation is stored.

ERRMSG=err-var

err-var is a scalar default character value in which an error condition is stored if such a condition occurs.

SOURCE=source-expr
MOLD=source-expr

source-expr is an expression that is scalar or has the same rank as object. If source-expr is specified, type cannot be specified and each object must have the same rank as source-expr.

Description

The storage space allocated is uninitialized unless SOURCE= is specified.

A bound in s-spec must not be an expression containing an array inquiry function whose argument is any allocatable object in the same ALLOCATE statement; for example, the following is not permitted:

INTEGER ERR
INTEGER, ALLOCATABLE :: A(:), B(:)
...
ALLOCATE(A(10:25), B(SIZE(A)), STAT=ERR)  ! A is invalid as an argument to function SIZE

If object is an array, s-spec must appear or source-expr must appear and have the same rank as object. If object is scalar, no s-spec should appear.

If a STAT= variable, ERRMSG= variable, or source-expr is specified, it must not be allocated in the ALLOCATE statement in which it appears; nor can they depend on the value, bounds, length type parameters, allocation status, or association status of any object in the same ALLOCATE statement.

If the allocation is successful for STAT= or ERRMSG=, the variable is set to zero. If the allocation is not successful, an error condition occurs, and the variable is set to a positive integer value (representing the run-time error); the ERRMSG= variable contains the error condition.

If no STAT= variable is specified and an error condition occurs, program execution terminates.

If the allocation is successful and source-expr is specified, the dynamic type and value of the allocated object becomes that of the source expression. If the value of a non-deferred length type parameter of object is different from the value of the corresponding type parameter of source-expr, an error condition occurs.

If any object has a deferred type parameter, is unlimited polymorphic, or is of ABSTRACT type, either type or source-expr must appear.

If object is a coarray, source-expr must not have a dynamic type of C_PTR, C_FUNPTR, or LOCK_TYPE, or have a subcomponent whose dynamic type is LOCK_TYPE. The object cannot be a coindexed object.

When an ALLOCATE statement is executed for an object that is a coarray, there is an implicit synchronization of all images. On each image, execution of the segment following the statement is delayed until all other images have executed the same statement the same number of times.

If an ALLOCATE or DEALLOCATE statement with a coarray allocatable object is executed when one or more images has initiated termination of execution, the STAT= variable becomes defined with the processor-dependent positive integer value of the constant STAT_STOPPED_IMAGE from the intrinsic module ISO_FORTRAN_ENV.

If any other error condition occurs during execution of the ALLOCATE or DEALLOCATE statement, the STAT= variable becomes defined with a processor-dependent positive integer value different from STAT_STOPPED_IMAGE.

If MOLD= appears and source-expr is a variable, its value does not have to be defined.

To release the storage for an object, use DEALLOCATE.

To determine whether an allocatable object is currently allocated, use the ALLOCATED intrinsic function.

To determine whether a pointer is currently associated with a target, use the ASSOCIATED intrinsic function.

Example

  !Method for creating and allocating deferred shape arrays.
  INTEGER,ALLOCATABLE::matrix(:,:)
  REAL, ALLOCATABLE:: vector(:)
  . . .
  ALLOCATE (matrix(3,5),vector(-2:N+2))
  . . .

The following shows another example of the ALLOCATE statement:

  INTEGER J, N, ALLOC_ERR
  REAL, ALLOCATABLE :: A(:), B(:,:)
  ...
  ALLOCATE(A(0:80), B(-3:J+1, N), STAT = ALLOC_ERR)

The above example allocates the scalar objects s and t to be 15 by 25 matrices with the value of r.

Consider the following:

REAL(KIND=8) :: r(15, 25)
REAL(KIND=8), ALLOCATABLE :: s(:,:), t(:,:)
...
ALLOCATE(s,SOURCE=r)
ALLOCATE(t,SOURCE=r)

The above example allocates s and t to be 15 by 25 arrays with the value of r.

See Also