Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Fortran > Re: Derived typ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 2 of 26 Topic 8212 of 8908
Post > Topic >>

Re: Derived types and allocatable

by Gib Bogle <g.bogle@[EMAIL PROTECTED] > May 6, 2008 at 06:09 PM

I must be more tired than I thought.  The code I posted previously is
obviously wrong, in a couple 
of embarrassing ways, and the reason for the access violation is clear. 
What I meant to do is shown 
below.  This code executes fine.  I thought I was on the scent of the bug
in my real program, but I 
was confused.  The interesting thing (to me) here is that the assignment
cell2(i) = cell1(i) has the 
effect of allocating cell2(i)%cdata.  Should this be obvious?

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 before this
     write(*,*) cell2(i)%cdata
enddo

do i = 1,2
     if (allocated(cell1(i)%cdata)) then
         deallocate(cell1(i)%cdata)
     endif
enddo
if (allocated(cell1)) then
     deallocate(cell1)
endif
do i = 1,2
     if (allocated(cell2(i)%cdata)) then
         write(*,*) 'deallocate cell2(i)%cdata: ',i
         deallocate(cell2(i)%cdata)
     endif
enddo
if (allocated(cell2)) then
     deallocate(cell2)
endif

end
 




 26 Posts in Topic:
Derived types and allocatable
Gib Bogle <g.bogle@[EM  2008-05-06 16:01:28 
Re: Derived types and allocatable
Gib Bogle <g.bogle@[EM  2008-05-06 18:09:36 
Re: Derived types and allocatable
"James Van Buskirk&q  2008-05-06 00:41:26 
Re: Derived types and allocatable
Steven Correll <steven  2008-05-06 07:08:11 
Re: Derived types and allocatable
"James Giles" &  2008-05-11 00:26:34 
Re: Derived types and allocatable
=?ISO-8859-15?Q?Jan_Vorbr  2008-05-14 12:07:46 
Re: Derived types and allocatable
"James Giles" &  2008-05-14 19:30:39 
Re: Derived types and allocatable
glen herrmannsfeldt <g  2008-05-14 12:39:38 
Re: Derived types and allocatable
"James Giles" &  2008-05-14 20:49:59 
Re: Derived types and allocatable
glen herrmannsfeldt <g  2008-05-14 13:27:46 
Re: Derived types and allocatable
"James Giles" &  2008-05-14 21:40:48 
Re: Derived types and allocatable
=?ISO-8859-15?Q?Jan_Vorbr  2008-05-15 11:14:13 
Re: Derived types and allocatable
"James Van Buskirk&q  2008-05-14 11:03:21 
Re: Derived types and allocatable
"James Giles" &  2008-05-14 19:37:02 
Re: Derived types and allocatable
"Kurt Kallblad"  2008-05-07 07:56:49 
Re: Derived types and allocatable
nospam@[EMAIL PROTECTED]   2008-05-07 00:15:43 
Re: Derived types and allocatable
Gib Bogle <bogle@[EMAI  2008-05-08 08:35:27 
Re: Derived types and allocatable
Damian <damian@[EMAIL   2008-05-07 12:03:34 
Re: Derived types and allocatable
Steve Lionel <Steve.Li  2008-05-07 15:33:13 
Re: Derived types and allocatable
Damian <damian@[EMAIL   2008-05-08 09:05:09 
Re: Derived types and allocatable
Steve Lionel <Steve.Li  2008-05-08 12:52:00 
Re: Derived types and allocatable
Damian <damian@[EMAIL   2008-05-08 19:56:55 
Re: Derived types and allocatable
Steve Lionel <Steve.Li  2008-05-09 14:11:34 
Re: Derived types and allocatable
Damian <damian@[EMAIL   2008-05-09 12:15:07 
Re: Derived types and allocatable
"James Van Buskirk&q  2008-05-06 00:11:29 
Re: Derived types and allocatable
Gib Bogle <g.bogle@[EM  2008-05-06 18:39:41 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Wed Nov 19 7:45:41 CST 2008.