Purpose
The ALLOCATE statement dynamically provides storage for pointer targets and allocatable arrays.
Format
>>-ALLOCATE--(--allocation_list--+------------------------------+---)--> '-,--STAT-- = --stat_variable--' >-------------------------------------------------------------->< |
allocation
>>-allocate_object--+--------------------------------------------------+-> | .-,------------------------------------. | | V | | '-(-----+-----------------+---upper_bound---+---)--' '-lower_bound--:--' >-------------------------------------------------------------->< |
Rules
Execution of an ALLOCATE statement for a pointer causes the pointer to become associated with the target allocated. For an allocatable array, the array becomes definable.
The number of dimensions specified (i.e., the number of upper bounds in allocation) must be equal to the rank of allocate_object. When an ALLOCATE statement is executed for an array, the values of the bounds are determined at that time. Subsequent redefinition or undefinition of any entities in the bound expressions does not affect the array specification. Any lower bound, if omitted, is assigned a default value of 1. If any lower bound value exceeds the corresponding upper bound value, that dimension has an extent of 0 and allocate_object is zero-sized.
A specified bound must not be an expression that contains as a primary an array inquiry function whose argument is an allocate_object in the same ALLOCATE statement. The stat_variable must not be allocated within the ALLOCATE statement in which it appears; nor can it depend on the value, bounds, allocation status, or association status of any allocate_object or subobject of an allocate_object allocated in the same statement.
If the STAT= specifier is not present and an error condition
occurs during execution of the statement, the program terminates. If
the STAT= specifier is present, the stat_variable is
assigned one of the following values:
Stat value | Error condition |
---|---|
0 | No error |
1 | Error in system routine attempting to do allocation |
2 | An invalid data object has been specified for allocation |
3 | Both error conditions 1 and 2 have occurred |
Allocating an allocatable array that is already allocated causes an error condition in the ALLOCATE statement.
Pointer allocation creates an object that has the TARGET attribute. Additional pointers can be associated with this target (or a subobject of it) through pointer assignment. If you reallocate a pointer that is already associated with a target:
Use the ALLOCATED intrinsic function to determine if an allocatable array is currently allocated. Use the ASSOCIATED intrinsic function to determine the association status of a pointer or whether a pointer is currently associated with a specified target.
Examples
CHARACTER, POINTER :: P(:,:) CHARACTER, TARGET :: C(4,4) INTEGER, ALLOCATABLE, DIMENSION(:) :: A P => C N = 2; M = N ALLOCATE (P(N,M),STAT=I) ! P is no longer associated with C N = 3 ! Target array for P maintains 2X2 shape IF (.NOT.ALLOCATED(A)) ALLOCATE (A(N**2)) END
Related Information