Google Groups Home Help | Sign in
Message from discussion Derived types and allocatable
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Gib Bogle  
View profile
 More options May 6, 12:01 am
Newsgroups: comp.lang.fortran
From: Gib Bogle <g.bo...@auckland.no.spam.ac.nz>
Date: Tue, 06 May 2008 16:01:28 +1200
Local: Tues, May 6 2008 12:01 am
Subject: Derived types and allocatable
I am still trying to get to grips with how one should copy derived types that contain allocatable
arrays.  The test program below, when compiled with IVF 10.1, displays the cdata array contents
correctly, but gives an access violation in the deallocation step.  If I neglect the deallocation
there are no error messages.  When compiled on IBM Linux (with xlf95_r) it runs without errors.  But
running my real program, in which I am presumably doing something wrong, on the IBM machine there is
no error until the deallocation step, which is carried out exactly as in the code below, and then I
get a multitude of error messages like

glibc detected *** double free or corruption (!prev): 0x100a0220
and
glibc detected *** corrupted double-linked list: 0x1009db18

Is it OK to do

cell2(i) = cell1(i)

when cell2(i)%cdata has not been allocated, or do I first need to ensure that %cdata is allocated?

!----------------------------------------------------------------
program main
integer :: csize(2) = (/10,100/)
integer :: k, i
type cell_type
     integer :: ID
     real, allocatable :: cdata(:)
end type
type(cell_type), allocatable :: cell1(:),cell2(:)

allocate(cell1(2))
allocate(cell2(2))

do i = 1,2
     allocate(cell1(i)%cdata(csize(i)))
     do k = 1,csize(i)
         cell1(i)%cdata(k) = k
     enddo
enddo

do i = 1,2
     cell2(i) = cell1(i)     ! cell2(i)%cdata not allocated
     write(*,*) cell2(i)%cdata
enddo

do i = 1,2
     do k = 1,csize(i)
         if (allocated(cell1(i)%cdata)) then
             deallocate(cell1(i)%cdata)
         endif
     enddo
     if (allocated(cell1)) then
         deallocate(cell1)
     endif
enddo
do i = 1,2
     do k = 1,csize(i)
         if (allocated(cell2(i)%cdata)) then
             deallocate(cell2(i)%cdata)
         endif
     enddo
     if (allocated(cell2)) then
         deallocate(cell2)
     endif
enddo

end
------------ And now a word from our sponsor ------------------
Want to have instant messaging, and chat rooms, and discussion
groups for your local users or business, you need dbabble!
--  See http://netwinsite.com/sponsor/sponsor_dbabble.htm  ----


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google