Re: Allocatable arrays in derived types
- From: Arjen Markus <arjen.markus@xxxxxxxxxx>
- Date: Tue, 29 Apr 2008 01:23:58 -0700 (PDT)
On 29 apr, 09:57, Gib Bogle <bo...@xxxxxxxxxxxxxxxxxxxxxxxx> 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 reported size of cell
is still 8 integers. The allocated memory is _not_
transferred.
Regards,
Arjen
.
- Follow-Ups:
- Re: Allocatable arrays in derived types
- From: relaxmike
- Re: Allocatable arrays in derived types
- References:
- Allocatable arrays in derived types
- From: Gib Bogle
- Allocatable arrays in derived types
- Prev by Date: Allocatable arrays in derived types
- Next by Date: Re: Allocatable arrays in derived types
- Previous by thread: Allocatable arrays in derived types
- Next by thread: Re: Allocatable arrays in derived types
- Index(es):
Relevant Pages
|