LF Fortran 95

ALLOCATE Statement


The ALLOCATE statement dynamically creates storage for array variables having the ALLOCATABLE or POINTER attribute. If the object of an ALLOCATE statement is a pointer, execution of the ALLOCATE statement causes the pointer to become associated. If the object of an ALLOCATE statement is an array, the ALLOCATE statement defines the shape of the array.


ALLOCATE (allocation-list [, STAT=stat-variable])


allocation-list is a comma-separated list of pointer or allocatable variables. Each allocatable or pointer array in the allocation-list will have a list of dimension bounds, ( [lower-bound :] upper-bound [, ...] )
upper bound and lower-bound are scalar INTEGER expressions.
stat-variable is a scalar INTEGER variable.


When the ALLOCATE statement is executed, the number of dimensions being allocated must agree with the declared rank of the array.

If the optional STAT= is present and the ALLOCATE statement succeeds, stat-variable is assigned the value zero. If STAT= is present and the ALLOCATE statement fails, stat-variable is assigned the number of the error message generated at runtime.

If an error condition occurs during execution of an ALLOCATE statement that does not contain the STAT= specifier, execution of the program is terminated.

The default lower-bound value is one.

If upper-bound is less than lower-bound, the extent of that dimension is zero and the entire array has zero size.

The ALLOCATED intrinsic function determines whether an allocatable array is currently allocated.

The ASSOCIATED intrinsic function determines whether a pointer is currently associated with a target.

Attempting to allocate a currently allocated variable causes an error condition to occur.

If a pointer that is currently associated with a target is allocated, a new pointer target is created and the pointer is associated with that target. If there is no other reference to the original target, the storage associated with the original target is lost and cannot be recovered.

If local allocatable or pointer arrays do not have the SAVE attribute, they may be automatically deallocated upon execution of a RETURN statement.

Example 1

integer,pointer,dimension(:,:) :: i => null() integer,allocatable,dimension (:) :: j integer,pointer :: k ! scalar pointer write(*,*) associated(i), associated(k) ! writes F F allocate (i(10,20),k) write(*,*) associated(i), shape(i) ! writes T 10 20 write(*,*) associated(k) ! writes T deallocate (i,k) write(*,*) allocated(j) ! writes F allocate (j(10)) write(*,*) allocated(j), shape(j) ! writes T 10 deallocate (j) ! space deallocated

Example 2

integer :: alloc_stat real,allocatable,dimension (:) :: r write(*,*) allocated(r) ! writes F allocate (r(10),stat=alloc_stat) write(*,*) allocated(r),alloc_stat ! writes T 0 allocate (r(20),stat=alloc_stat) write(*,*) allocated(r),alloc_stat ! writes T 1001 deallocate (r) ! space deallocated allocate (r(20:-20),stat=alloc_stat) ! zero size array write(*,*) size(r),shape(r),alloc_stat ! writes 0 0 0

See Also

Allocated Function

Deallocate Statement