Re: Allocatable arrays in derived types



Let me expand a bit on Richard's comment

relaxmike wrote:
| Hi,
|
| I don't remember exactly where I read it on this forum,
| but I remember that the difference between allocatable and pointer
| arrays is that, when an allocatable array goes out of scope,
| it is automatically deallocated.
| A pointer which goes out of scope is not automatically deallocated,
| because it may happen that another pointer keeps a reference
| to the pointee, so that an automatic deallocation would lost
| the reference.
| This is the main reason for the overhead of a allocatable
| array (and is also the main reason for some bugs in the
| implementation of allocatable in fortran compilers, which is
| tricky in complicated situations, like derived type, recursive
| function, etc...).

No. The overhead of an allocatable array in a typical implementation
is exactly the same as the one for a pointer of the same rank. And
in turn, those "overheads" are [tend to be] the same as for passing
and handling an assumed-shape array.

The primary cause of the overhead is complexity of Fortran arrays:
they can have multiple dimensions; they may start at index other
than default (1); they are of known size; the may have non-unit
strides... etc. Not all Fortran array types have all those properties
(e.g. ALLOCATABLEs cannot have non-unit stride), but it's convenient
for compiler writers to implement them using the same data structure
(known as "array descriptor" or "dope vector"). Those 7 or 8
dwords typically contain the starting address, a couple of flags,
lower bound, upper bound or size, and stride per each dimension.

Note that it's not releated with "bookkeeping" in "reference
counting" sense of the word. Fortran does not require (nor
common implementations perform) garbage collection, i.e.
"bookkeeping" how many pointers use the watched memory and
cleanup when the count reaches zero. Such bookkeeping would
probably have to be done outside of the array descriptors
anyway (but I'm not an expert on that).

--
Jugoslav
___________
www.xeffort.com

Please reply to the newsgroup.
You can find my real e-mail on my home page above.
.



Relevant Pages

  • Re: Q about simple program, with suggestions
    ... It's a pointer pointing to an array w/ dimension 2 ... > END TYPE Matrix ... With allocatables in f2003, you could even drop the second ...
    (comp.lang.fortran)
  • Re: char **argv & char *argv[]
    ... "pointer to pointer to char". ... >> pointer)) pointing to the first element of an array. ... so we have to start adding more context. ... type "pointer to char", rather than "array MISSING_SIZE of char". ...
    (comp.lang.c)
  • Re: multi dimensional arrays as one dimension array
    ... please - where does the standard say that such a conversion ... Pointer conversion yields a pointer to the same object as ... exist only where there are array declarations. ...
    (comp.lang.c)
  • Re: Evaluating unary *
    ... 'arr' exists, ... value can be used with the same syntax as would be used to access a 2D array of the kind you're referring to, but that 2D array is just a different way of looking as the same object that was already created by the definition of 'arr'. ... to me, it makes sense to return a pointer to the first value of an array, but to return the address of the pointer to the first value of an array, is not directly possible as such. ... lea eax, ...
    (comp.std.c)
  • Re: "Mastering C Pointers"....
    ... A pointer is a kind of variable that can "point to" some object. ... has a type (pointer to int), and a value of some kind. ... You may know that you can access these integers by using array notation ... The function will take one argument, a string, and will return the length ...
    (comp.lang.c)