For Programmers: Free Programming Magazines  


Home > Archive > Fortran > July 2004 > allocate / deallocate question









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author allocate / deallocate question
Les

2004-07-23, 8:57 am

I am working on part of a large CAD project making some data arrays dynamic
instead of static.
I copy the data to a temporary array, allocate the data array with its new
size, and copy the data back again. If the allocation of the newsize fails
then I wanted to re-allocate at the oldsize, and copy back the data.

In my destructive testing the "allocate(X3D(newsize),stat=iEr) " fails, but
then so does the attempt to re-allocate X3D at the oldsize.
I am trying to do a "graceful exit" but would like to recover the data that
was originally in X3D array.
Any helpful suggestions / workarounds/ corrections would be gratefully
received.

Thanks
Les

CVF 6.1A on Windows XP

Code snippet below


! Allocate temporary array
allocate(R8Temp(oldsize),stat=iEr)

if(iEr == 0) then
! Store current data in temporary array
R8Temp(1:oldsize)=X3D(1:oldsize)
! deallocate array of current data
deallocate(X3D,stat=iEr)

! ok now allocate new x3d array of newsize
if(iEr == 0) then
allocate(X3D(newsize),stat=iEr)

! if ok then copy data from temporary to new x3d array
if(iEr == 0) then
X3D(1:oldsize)=R8Temp(1:oldsize)
else
! failed. re-allocate x3d at oldsize and restore the data
allocate(X3D(oldsize),stat=iEr)
if(iEr == 0) then
X3D(1:oldsize)=R8Temp(1:oldsize)
endif
endif
endif

endif


Dick Hendrickson

2004-07-23, 8:57 am



Les wrote:
> I am working on part of a large CAD project making some data arrays dynamic
> instead of static.
> I copy the data to a temporary array, allocate the data array with its new
> size, and copy the data back again. If the allocation of the newsize fails
> then I wanted to re-allocate at the oldsize, and copy back the data.


Try using pointers instead of allocatable arrays. Something
like

allocate (new_data(newsize),stat = iEr)
if (iEr ==0)
new_data(1:oldsize) = X3D
deallocate (X3D)
X3D => new_data
endif

I think that uses one less copy of the data and might also
make garbage collection easier for the compiler.

Dick Hendrickson
>
> In my destructive testing the "allocate(X3D(newsize),stat=iEr) " fails, but
> then so does the attempt to re-allocate X3D at the oldsize.
> I am trying to do a "graceful exit" but would like to recover the data that
> was originally in X3D array.
> Any helpful suggestions / workarounds/ corrections would be gratefully
> received.
>
> Thanks
> Les
>
> CVF 6.1A on Windows XP
>
> Code snippet below
>
>
> ! Allocate temporary array
> allocate(R8Temp(oldsize),stat=iEr)
>
> if(iEr == 0) then
> ! Store current data in temporary array
> R8Temp(1:oldsize)=X3D(1:oldsize)
> ! deallocate array of current data
> deallocate(X3D,stat=iEr)
>
> ! ok now allocate new x3d array of newsize
> if(iEr == 0) then
> allocate(X3D(newsize),stat=iEr)
>
> ! if ok then copy data from temporary to new x3d array
> if(iEr == 0) then
> X3D(1:oldsize)=R8Temp(1:oldsize)
> else
> ! failed. re-allocate x3d at oldsize and restore the data
> allocate(X3D(oldsize),stat=iEr)
> if(iEr == 0) then
> X3D(1:oldsize)=R8Temp(1:oldsize)
> endif
> endif
> endif
>
> endif
>
>


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com