The Queen's University of Belfast
Parallel Computer Centre

[Next] [Previous] [Top]

10 Dynamic arrays


So far all variables that have been used have been static variables, that is they have had a fix memory requirement, which is specified when the variable is declared. Static arrays in particular are declared with a specified shape and extent which cannot change while a program is running. This means that when a program has to deal with a variable amount of data, either:

In contrast dynamic (or allocatable) arrays are not declared with a shape and initially have no associated storage, but may be allocated storage while a program executes. This is a very powerful feature which allows programs to use exactly the memory they require and only for the time they require it.

10.1 Allocatable arrays

10.1.1 Specification

Allocatable arrays are declared in much the same way as static arrays. General form:

type, ALLOCATABLE [,attribute] :: name

They must include the ALLOCATABLE attribute and the rank of the array, but cannot specify the extend in any dimension or the shape in general. Instead a colon (:) is used for each dimension. For example:

INTEGER, DIMENSION(:), ALLOCATABLE :: a !rank 1

INTEGER, ALLOCATABLE :: b(:,:) !rank 2

REAL, DIMENSION(:), ALLOCATABLE :: c !rank 1

On declaration, allocatable arrays have no associated storage and cannot be referenced until storage has been explicitly allocated.

10.1.2 Allocating and deallocating storage

The ALLOCATE statement associates storage with an allocatable array:

ALLOCATE( name(bounds) [,STAT] )

it is possible to allocate more than one array with the same ALLOCATE statement, each with different bounds, shape or rank. If no lower bound is specified then the default is 1. Only allocatable arrays with no associated storage may be the subject of an ALLOCATE statement, for example

n=10

ALLOCATE( a(100) )

ALLOCATE( b(n,n), c(-10:89) ).

The storage used by an allocatable array may be released at any time using the DEALLOCATE statement:

DEALLOCATE( name [,STAT] )

The DEALLOCATE statement does not require the array shape. It is possible to deallocate more than one array with the same DEALLOCATE statement, each array can have different bounds, shape or rank. Only allocatable arrays with associated storage may be the subject of a DEALLOCATE statement.

The following statements deallocate the storage from the previous example:

DEALLOCATE ( a, b )

DEALLOCATE ( c, STAT=test )

IF (test .NE. 0) THEN

STOP `deallocation error'

ENDIF

It is good programming practice to deallocate any storage that has been reserved through the ALLOCATE statement. Beware, any data stored in a deallocated array is lost permanently!

10.1.3 Status of allocatable arrays

Allocatable arrays may be in either one of two states:

The status of an array may be tested using the logical intrinsic function ALLOCATED:

AllOCATED( name )

which returns the value:

For example:

IF( ALLOCATED(x) ) DEALLOCATE( x )

or:

IF( .NOT. ALLOCATED( x ) ) ALLOCATE( x(1:10) )

On declaration an allocatable array's status is `not currently allocated' and will become `allocated' only after a successful ALLOCATE statement. As the program continues and the storage used by a particular array is deallocated, so the status of the array returns to `not currently allocated'. It is possible to repeat this cycle of allocating and deallocating storage to an array (possibly with different sizes and extents each time) any number of times in the same program.

10.2 Memory leaks

Normally, it is the program that takes responsibility for allocating and deallocating storage to (static) variables, however when using dynamic arrays this responsibility falls to the programmer.

Statements like ALLOCATE and DEALLOCATE are very powerful. Storage allocated through the ALLOCATE statement may only be recovered by:

Storage allocated to local variables (in say a subroutine or function) must be deallocated before the exiting the procedure. When leaving a procedure all local variable are deleted from memory and the program releases any associated storage for use elsewhere, however any storage allocated through the ALLOCATE statement will remain `in use' even though it has no associated variable name!. Storage allocated, but no longer accessible, cannot be released or used elsewhere in the program and is said to be in an `undefined' state This reduction in the total storage available to the program called is a `memory leak'.

SUBROUTINE swap(a, b)

REAL, DIMENSION(:) :: a, b

REAL, ALLOCATABLE :: work(:)

ALLOCATE( work(SIZE(a)) )

work = a

a = b

b = work

DEALLOCATE( work ) !necessary

END SUBROUTINE swap

The automatic arrays a and b are static variables, the program allocates the required storage when swap is called, and deallocates the storage on exiting the procedure. The storage allocated to the allocatable array work must be explicitly deallocated to prevent a memory leak.

Memory leaks are cumulative, repeated use of a procedure which contains a memory leak will increase the size of the allocated, but unusable, memory. Memory leaks can be difficult errors to detect but may be avoided by remembering to allocate and deallocate storage in the same procedure.

10.3 Exercises

  1. Write a declaration statement for each of the following allocatable arrays:

    (a) Rank 1 integer array.

    (b) A real array of rank 4.

    (c) Two integer arrays one of rank 2 the other of rank 3.

    (d) A rank one real array with lower and upper bound of -n and n respectively.

  2. Write allocation statements for the arrays declared in question 1, so that

    (a) The array in 1 (a) has 2000 elements

    (b) The array in 1 (b) has 16 elements in total.

    (c) In 1 (c) the rank two array has 10 by 10 elements, each index starting at element 0; and the rank three array has 5 by 5 by 10 elements, each index starting at element -5.

    (d) The array in 1 (d) is allocated as required.

  3. Write deallocation statement(s) for the arrays allocated in 2.

  4. Write a program to calculate the mean and the variance of a variable amount of data. The number of values to be read into a real, dynamic array x is n. The program should use a subroutine to calculate the mean and variance of the data held in x. The mean and variance are given by:

  5. Write a module called tmp_space to handle the allocation and deallocation of an allocatable work array called tmp. The module should contain two subroutines, the first (make_tmp) to deal with allocation, the second (unmake_tmp) to deal with deallocation. These subroutines should check the status of tmp and report any error encountered. Write a program that tests this module.

    The idea behind such a module is that once developed it may be used in other programs which require a temporary work array.


[Next] [Previous] [Top]
All documents are the responsibility of, and copyright, © their authors and do not represent the views of The Parallel Computer Centre, nor of The Queen's University of Belfast.
Maintained by Alan Rea, email A.Rea@qub.ac.uk
Generated with CERN WebMaker