Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Allocatable Arrays

chris_hawkins
Beginner
831 Views
Is there a method to resize AN EXISTING dynamic array without trashing the existing contents? i.e. similar to "REDIM PRESERVE" in Visual Basic. The only way that I can think of is to copy the contenst into a temporary array of the same dimensions / size, deallocate the original array, allocate it again and then copy the contents of the tempoary array back. Seems somewhat ineligant!
0 Kudos
3 Replies
Jugoslav_Dujic
Valued Contributor II
831 Views
No -- but CVF now supports ALLOCATABLE arguments (which is a feature from Fortran 2000 draft), so you can solve it more elegantly and generally:
SUBROUTINE ReAlloc(A, n)
REAL, ALLOCATABLE:: A(:)
INTEGER::           n, nOld
REAL, ALLOCATABLE:: Temp(:)
nOld = SIZE(Temp)
IF (nOld > n) RETURN
ALLOCATE(Temp(nOld))
Temp=A
DEALLOCATE(A)
ALLOCATE(A(n))
A(1:nOld) = Temp
END SUBROUTINE Realloc

Now you can reallocate any array in a single subroutine call.

Jugoslav
0 Kudos
Intel_C_Intel
Employee
831 Views
Shouldn't it be nOld=SIZE(A), not

> nOld = SIZE(Temp)

Eddie
0 Kudos
chris_hawkins
Beginner
831 Views
Thanks - I arrived at almost the same solution but at least I know that I wasn't missing something. CJH
0 Kudos
Reply