The Queen's University of Belfast
Parallel Computer Centre

[Next] [Previous] [Top]

Dynamic arrays


Dynamic arrays

Topics

Static arrays

Recall

Declaration statements for static variables allocate storage (a block of memory):

INTEGER, DIMENSION(10) :: x

Variable sized arrays

Assumed shape and automatic arrays

Recall

Arrays in a procedure can accept different sized array arguments on each call:

SUBROUTINE sub( a )

REAL, DIMENSION(:) :: a !assumed

REAL :: b( SIZE(a) ) !automatic

Allocatable Arrays

Specification

type, ALLOCATABLE [, attr] :: name

REAL, DIMENSION(:), ALLOCATABLE :: a

INTEGER, ALLOCATABLE :: b(:,:)

REAL, ALLOCATABLE, DIMENSION(:) :: c

Allocating & deallocating storage

ALLOCATE( name(size) [, STAT] )

Beware: deallocated data is lost permanently!

Example

INTEGER, ALLOCATABLE :: a(:)

INTEGER, ALLOCATABLE :: b(:,:)

REAL, ALLOCATABLE :: c(:)

INTEGER :: n=10

ALLOCATE( a(100) )

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

...

DEALLOCATE ( a, b )

DEALLOCATE ( c, STAT=test )

IF (test .NE. 0) THEN

STOP `deallocation error'

ENDIF

Each of the above allocation statements give the array in question 100 elements.

Status

An allocatable array may be in one of two states:

The status of an array may be tested using:

ALLOCATED( name )

IF( ALLOCATED( a ) ) DEALLOCTE( a )

Memory leaks

`Undefined' storage

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




[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