- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Not until the 2003 Fortran standard ReAllocate capability is provided.
The followingshows a (cut down version of a) technique I use a lot usingmodules,pointers and allocatable arrays :
Code:
Module X
! Assign appropriate values to the following 3 integers
integer*4 :: InitialSize
integer*4 :: CurrentSize
integer*4 :: Increment
! Pointer and target arrays
real*8, pointer :: zlevel(:) ! Z Level
real*8, allocatable, target :: zlevel_a(:) ! Z Level
real*8, allocatable, target :: zlevel_b(:) ! Z Level
CONTAINS
subroutine AllocateZLevel
! having set an appropriate value for InitialSize
allocate(zlevel_a(InitialSize),stat=ierr)
! Point zlevel to zlevel_a
zlevel => zlevel_a
! Initialise zlevel to some appropriate starting value
zlevel = 0.0.d0
CurrentSize = InitialSize
end subroutine AllocateZLevel
subroutine ReAllocateZLevel(ierr)
osize = CurrentSize
nsize = osize + INCREMENT
! Check which array is currently allocated a or b
! Allocate the other with the new size
! Copy the data from old array to the new array
! Point the pointer to the new array
if (allocated(zlevel_b)) then
allocate(zlevel_a(nsize), stat=ierr)
if (ierr == 0) then
do i=1,osize
zlevel_a(i) = zlevel(i)
enddo
deallocate(zlevel_b, stat=i)
zlevel => zlevel_a
endif
else
allocate(zlevel_b(nsize), stat=ierr)
if (ierr == 0) then
do i=1,osize
zlevel_b(i) = zlevel(i)
enddo
deallocate(zlevel_a, stat=i)
zlevel => zlevel_b
endif
endif
if (ierr == 0) then
CurrentSize = nsize
endif
return
end subroutine ReAllocateZLevel
end module X
I hope you see the principle anyway. Thedata is referenced throughout the code by zlevel and zlevel points to either zlevel_a or zlevel_bdepending on the calls to ReAllocateZLevel
Les
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Sorry for the misinformation. I thought I remembered something about the programmerbeing able to reallocate arrays (in place?)in F2003 I wasn't sure ifthere was a function/subroutine to do it though, or whether it wasan automatic "behind the scene" thing on assignment.
Reading more closelythe document"New features of Fortran 2003" by John Reid I see the use of MOVE_ALLOC.
Thanks Steve for the correction
Les
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page