LF Fortran 95

Dynamic Arrays

An array can be fixed in size at compile time or can assume a size or shape at run time in a number of ways:

Allocatable Arrays

The ALLOCATABLE attribute can be given to an array in a type declaration statement or in an ALLOCATABLE statement. An allocatable array must be declared with the deferred-shape specifier, `:', for each dimension. For example,

integer, allocatable :: a(:), b(:,:,:)

declares two allocatable arrays, one of rank one and the other of rank three.

The bounds, and thus the shape, of an allocatable array are determined when the array is allocated with an ALLOCATE statement. Continuing the previous example,

allocate (a(3), b(1,3,-3:3))

allocates an array of rank one and size three and an array of rank three and size 21 with the lower bound -3 in the third dimension.

Memory for allocatable arrays is returned to the system using the DEALLOCATE statement.

Array Pointers

The POINTER attribute can be given to an array in a type declaration statement or in a POINTER statement. An array pointer, like an allocatable array, is declared with the deferred-shape specifier, `:', for each dimension. For example

integer, pointer, dimension(:,:) :: c

declares a pointer array of rank two. An array pointer can be allocated in the same way an allocatable array can. Additionally, the shape of a pointer array can be set when the pointer becomes associated with a target in a pointer assignment statement. The shape then becomes that of the target.

integer, target, dimension(2,4) :: d
integer, pointer, dimension(:,:) :: c
c => d

In the above example, the array c becomes associated with array d and assumes the shape of d.

Assumed-Shape Arrays

An assumed-shape array is a dummy array that assumes the shape of the corresponding actual argument. The lower bound of an assumed-shape array can be declared and can be different from that of the actual argument array. An assumed-shape specification is

[lower-bound] :

for each dimension of the assumed-shape array. For example

...
integer :: a(3,4)
...
call zee(a)
...
 
subroutine zee(x)
implicit none
integer, dimension(-1:,:) :: x
...

Here the dummy array x assumes the shape of the actual argument a with a new lower bound for dimension one.

The interface for an assumed-shape array must be explicit (see See Explicit Interfaces).

Assumed-Size Arrays

An assumed-size array is a dummy array that's size is not known. All bounds except the upper bound of the last dimension are specified in the declaration of the dummy array. In the declaration, the upper bound of the last dimension is an asterisk. The two arrays have the same initial array element, and are storage associated.

You must not refer to an assumed-size array in a context where the shape of the array must be known, such as in a whole array reference or for many of the transformational array intrinsic functions. A function result can not be an assumed-size array.

...
integer a
dimension a(4)
...
call zee(a)
...
 
subroutine zee(x)
integer, dimension(-1:*) :: x
...

In this example, the size of dummy array x is not known.

Adjustable and Automatic Arrays

You can establish the shape of an array based on the values of dummy arguments. If such an array is a dummy array, it is called an adjustable array. If the array is not a dummy array it is called an automatic array. Consider the following example:

integer function bar(i,k)
integer :: i,j,k
dimension i(k,3), j(k)
...

Here the shapes of arrays i and j depend on the value of the dummy argument k. i is an adjustable array and j is an automatic array.