ALLOCATE

Purpose

The ALLOCATE statement dynamically provides storage for pointer targets and allocatable objects.

Syntax

Read syntax diagramSkip visual syntax diagram>>-ALLOCATE----------------------------------------------------->
 
>--(--allocation_list--+-----------------------------+--)------><
                       '-,--STAT-- = --stat_variable-'
 
stat_variable
is a scalar integer variable

allocation_list

Read syntax diagramSkip visual syntax diagram>>-allocate_object--+-------------------------------------------+-><
                    |    .-,-------------------------------.    |
                    |    V                                 |    |
                    '-(----+----------------+--upper_bound-+--)-'
                           '-lower_bound--:-'
 

allocate_object
is a variable name or structure component. It must be a data pointer or an allocatable object.
lower_bound, upper_bound
are each scalar integer expressions

Rules

Execution of an ALLOCATE statement for a pointer causes the pointer to become associated with the target allocated. For an allocatable object, the object 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.

Fortran 2003 Standard

Any allocate_object or a specified bound of an allocate_object must not depend on stat_variable or on the value, bounds, allocation status, or association status of any allocate_object in the same ALLOCATE statement.

stat_variable must not be allocated within the ALLOCATE statement in which they appear. They also must not depend on the value, bounds, length type parameters, allocation status, or association status of any allocate_object in the same ALLOCATE statement.

End of Fortran 2003 Standard

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:

IBM Extension

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
End of IBM Extension

Allocating an allocatable object 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:

When an object of derived type is created by an ALLOCATE statement, any allocatable ultimate components have an allocation status of not currently allocated.

Use the ALLOCATED intrinsic function to determine if an allocatable object 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