ALLOCATE statement
Standard: F77 F90 F95 F2003 F2008 Example program
<Syntax>
ALLOCATE ( [ type-spec :: ] item-list [ , alloc-opt ]... )
item-list ::= item [ , item ]...
item ::= variable [ ( shape ) ]
shape ::= bounds [ , bounds ]...
alloc-opt | ::= | ERRMSG = variable |
| | MOLD = expression | |
| | SOURCE = expression | |
| | STAT = variable |
Each item must have the ALLOCATABLE
or POINTER
attribute.
If an item is an array, it must be followed by a shape with the same number of bounds as the rank of the item,
The ERRMSG= variable must be a scalar character variable of default kind. The STAT= variable must be a scalar integer variable, and should be at least 16 bits in size.
SOURCE= and type-spec are mutually exclusive.
<Semantics>
Allocates memory for a list of pointers and allocatable variables. If an item is allocatable, it must not be currently allocated (or an error will occur).
The shape is required for each item that is an array, with the same number of bounds as the rank of the array, unless the MOLD= or SOURCE= clause appears with an array expression. If the lower bound expression is omitted, the lower bound will be 1.
If an error occurs, e.g. an allocatable item is already allocated or there is not enough memory available, the program will be terminated unless the STAT= clause is present. The STAT= variable is set to zero if all allocations succeed, or to an error code if one of them fails.
In Fortran 2003, there are several additions.
- The ERRMSG=variable clause has been added; this sets a character string variable to an error message if an allocation fails. (This variable is unchanged if there is no error, so STAT= is still needed to prevent termination.)
- The SOURCE=expression clause has been added; this sets the value (and type if polymorphic) of the item to that of the source expression. In this case, Fortran 2003 only permits a single item to be allocated at a time.
- The item-list can be preceded by a type-spec; this sets the type of a polymorphic item.
In Fortran 2008, there are more additions.
-
The MOLD=expression has been added; this sets the type of a polymorphic allocation to be the same as the dynamic type of expression; the expression is permitted to be an undefined variable, since the value of the variable is not used.
Any default initialisation for the type will be applied to the new object.
If MOLD= appears, neither SOURCE= nor type-spec may appear.
- If MOLD= or SOURCE= appears with an expression that is an array, the bounds can be omitted from the array variable. In this case, the allocated bounds will be the same as those of the expression.
- More than one item is permitted to be allocated by an ALLOCATE statement with a SOURCE= clause.
<Related>
ALLOCATABLE attribute, ALLOCATED intrinsic function, ASSOCIATED intrinsic function, DEALLOCATE statement, ERRMSG= specifier, LBOUND intrinsic function, MOLD= specifier, MOVE_ALLOC intrinsic subroutine, POINTER attribute, SHAPE intrinsic function, SIZE intrinsic function, SOURCE= specifier, STAT= specifier, UBOUND intrinsic function