Re: Allocatable arrays in derived types



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
.



Relevant Pages

  • Re: Allocatable arrays in derived types
    ... type:: acell ... uncertain about how/where mydata is stored. ... When cell is allocated ... if you allocate cell%mydata the reported size of cell ...
    (comp.lang.fortran)
  • Re: Cons cell archaic!?
    ... from s-expression or XML or other syntax you keep the bloated array ... For using vectors to emulate lists that ... Allocate 2, move 1 element: ... What do you think of that algorithm? ...
    (comp.lang.lisp)
  • Storing/Retrieving TYPEs with ALLOCATABLE components (TR) (long)
    ... tBrd, including array descriptor of tEn )). ... Without previous DEALLOCATE, the allocate line fails at run time with message ... the fact that I'm loading an invalid descriptor tBrd%tEn from the file... ... status (which is not possible according to Standard, but then BINARY files ...
    (comp.lang.fortran)
  • Re: Storing the size of an array in the structure itself
    ... >> I think every C programmer can relate to the frustrations that malloc ... >> the size of an array must be stored separately to be a nightmare. ... is anything more than just that - a chunk of memory. ... > Otherwise you couldn't tell it how much to allocate. ...
    (comp.lang.c)
  • determining available space for Float32, for instance
    ... I am looking for a way to determine the maxium array size I can allocate ... We do not want a solution that requires recompiling Python, ... agents may be households that choose a new gridcell to live in. ... Each attribute of a dataset has such a 2D array. ...
    (comp.lang.python)