INTEGER, DIMENSION(10) :: x
Changing the size of this type of array requires re-compilation.
and is useful when:
SUBROUTINE sub( a )
REAL, DIMENSION(:) :: a !assumed
REAL :: b( SIZE(a) ) !automatic
SAVE
attribute.
type, ALLOCATABLE [, attr] :: name
REAL, DIMENSION(:), ALLOCATABLE :: a
INTEGER, ALLOCATABLE :: b(:,:)
REAL, ALLOCATABLE, DIMENSION(:) :: c
ALLOCATE( name(size) [, STAT] )
name
has the requested size (and STAT=0
if present).
STAT>0
if present).
DEALLOCATE( name [, STAT] )
name
has zero size (and STAT=0
if present).
STAT>0
if present).
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.
ALLOCATED( name )
.TRUE.
if the status of name
is `allocated'.
.FALSE.
if name
is `not currently allocated'.
IF( ALLOCATED( a ) ) DEALLOCTE( a )
No record of variable names are retained and storage is deallocated automatically.
ALLOCATE
statement over-rides a program's own management of memory.Recover allocated storage via:
DEALLOCATE
statement.
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