- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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!
Link Copied
3 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
No -- but CVF now supports ALLOCATABLE arguments (which is a feature from Fortran 2000 draft), so you can solve it more elegantly and generally:
Now you can reallocate any array in a single subroutine call.
Jugoslav
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Shouldn't it be nOld=SIZE(A), not
> nOld = SIZE(Temp)
Eddie
> nOld = SIZE(Temp)
Eddie
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks - I arrived at almost the same solution but at least I know that I wasn't missing something. CJH

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page