Intel® Fortran Compiler 18.0 Developer Guide and Reference
Statement: Dynamically creates storage for allocatable variables and pointer targets.
ALLOCATE ([type::] object[(s-spec[, s-spec]...)] [, object[(s-spec[, s-spec]...)] ]... [[coarray-spec]]...[, alloc-opt[, 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 and each object must be type compatible with the specified type. You cannot specify type if you specify SOURCE= or MOLD=. |
||||||
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. You can specify an s-spec list for each array object in an ALLOCATE statement. |
||||||
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 |
Can be any of the following keywords:
|
You can specify STAT=, ERRMSG=, and one of MOLD= or SOURCE= in the same ALLOCATE statement. The keywords can appear in any order.
The storage space allocated is uninitialized unless SOURCE= is specified or the type of the object is default initialized.
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 and the number of s-specs must equal the rank of object, or source-expr must appear and have the same rank as object and the shape of object is that of source-expr. If both s-spec and SOURCE=source-expr appear, source-expr must be scalar. If object is scalar, no s-spec should appear. If SOURCE=source-expr appears, object is initialized with the value of source-expr.
A type parameter value in type can be an asterisk if and only if each object is a dummy argument for which the corresponding type parameter is assumed.
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.
!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 values of r.