Arjen Markus wrote:
> On 29 apr, 09:57, Gib Bogle <bo...@[EMAIL PROTECTED]
> wrote:
>> Consider a derived type used thus:
>>
>> type mytype
>> integer :: a,b,c
>> real :: x,y,z
>> real, allocatable :: mydata(:)
>> end type
>>
>> type(mytype), allocatable :: cell(:)
>> type(mytype) :: acell
>>
>> allocate(cell(1000))
>> do i = 1,1000
>> if (i <= 500) then
>> allocate(cell(i)%mydata(10))
>> else
>> allocate(cell(i)%mydata(100))
>> endif
>> enddo
>>
>> acell = cell(100)
>> ...
>> acell = cell(600)
>>
>> Something like this seems to work fine, so I guess it's OK. I'm
>> uncertain about how/where mydata is stored. Perhaps I shouldn't even
be
>> thinking about this, but old habits die hard. When cell is allocated
>> how much space is reserved for mydata? I suppose this is
>> compiler-dependent, but what would be a typical number? When mydata is
>> allocated, where is it stored? Finally, when acell is equated to an
>> element of the cell array, where is its mydata?
>>
>> Thanks in advance for a free Fortran lesson :-)
>
> An allocatable array (just like a pointer to an array) is likely to
> have a bit of overhead - otherwise the size()
> function and friends would not work.
>
> If you simply allocate an array of cells but not the
> mydata field, then the mydata field will be unallocated.
> So I guess the overhead is in the array descriptor
> or whatever structure is used.
>
> Using this program:
>
> program extra_mem
>
> type cell_data
> integer, allocatable, dimension(:) :: mydata
> integer :: some_int
> end type cell_data
>
> type(cell_data) :: cell
>
> integer, dimension(1) :: dummy
>
> write(*,*) size(transfer(cell,dummy))
> end program
>
> with g95 as the compiler I get "8" as the answer to the
> size. So, I deduce that in this case each allocatable
> component involves 7 integers of extra memory.
>
> Note: if you allocate cell%mydata the re****ted size of cell
> is still 8 integers. The allocated memory is _not_
> transferred.
>
> Regards,
>
> Arjen
Thanks Arjen. If you answered the second part of my question, I didn't
understand (sorry). In my example code, on the statement
acell = cell(100)
does this allocate acell%mydata? I presume so, since
write(*,*) acell%mydata
gives the expected result. When acell is equated to cell(600) the write
again gives the expected result. From this I guess that the assignment
to acell allocates acell%mydata (deallocating it first if it is already
allocated) and copies cell(i)%mydata into it. Is this correct?
The reason I need to get a good grasp of what is happening here is that
my real program (which has the additional complication of being MPI
code) is exhibiting nasty Heisenbug behaviour, around a subroutine that
handles the transfer of data of such derived type between nodes. It is
a bit complicated because you can't construct an MPI type containing
allocatables or pointers, so I have to pull things apart to send then
reassemble them at the other end (I'm using MPICH2). A variable in the
calling program is clobbered (has its value changed) by the call to the
subroutine, although I have all runtime checks turned on in Intel
Fortran and no array overruns are detected. The program crashes with an
access violation shortly afterwards.


|