Re: parametrized derived types vs. allocatable components



m <mrestelli@xxxxxxxxx> wrote:

Hi,
I need to define an array of elements of derived type having in
turn an array component. The size of the array component is given at
run-time, but I know that it will be the same for all the elements of
the array. A possible solution would be:
[code using allocatable components elided]

I wonder whether one could use parametrized derived type as follows:
[mostly elided, except for relevant lines]
....
type tbvec(d)
....
type(tbvec(:)), allocatable :: bvec
....
allocate(bvec(dd))

The syntax on allocate is wrong there. That's the syntax for allocating
an array, which isn't (directly) what you are doing. Your bvec is a
scalar, even though it has array components. You want the form for
specifying a type and type parameters during allocate, which would be

allocate(tbvec(dd):: bvec)

Is xyz_pdt correct (I don't have any compiler to test it :-( )?

See above correction, but I don't have a suitable compiler either.

Would xyz_pdt allow a reference of the form:
bvec%vec(:)%x(1) ?

No. At least I doubt it. Someone else can check the citation if they
want, but this looks like something that "shouldn't" work. I think the
prohibitions are written generally enough to catch it. Probably
something about a designator not allowing allocatable or pointer things
to the right of a part with nonzero rank.

Does anybody see any advantage in one of the two forms (concerning for
instance efficiency or flexibility)?

Well, a big advantage of the allocatable components form is that most of
today's compilers support it. Parameterized derived types seem to be
pretty much the last f2003 feature to get implemented. I don't recall
that a single compiler has them yet. Implementation has lagged so much
that the question has at least been raised of whether to pull them from
the language. (That doesn't seem likely to happen, from what I read
here, but I no longer have a very good insight into some of the
committee ongoings; all my data is second hand and I'm not at all sure
that all I read is impartially reported).

A more nebulous advantage of allocatable components is that they seem to
me to be easier to read. I base that solely on looking at your example
codes; it isn't a comparison I had particularly thought to make before.
I just note that I seemed to have to reread your PDT example multiple
times to get it all straight. Perhaps it is just that I'm more used to
the allocatable components stuff. Or maybe I'm just not focussing well
at the moment and would find it different tomorrow. Who's to say? Take
this comment for whatever it might be worth.

--
Richard Maine | Good judgment comes from experience;
email: last name at domain . net | experience comes from bad judgment.
domain: summertriangle | -- Mark Twain
.



Relevant Pages

  • Re: efficiency of allocatable array into derived type
    ... I have made a simple comparison between three different derived types: 1) with static array, 2) with allocatable array, 3) pointer array. ... implicit none ... allocate, sb, sc) ... static and pointer array into derived type are at least one order slower than the static version; ...
    (comp.lang.fortran)
  • Re: a flexible number of derived type
    ... Arjen Markus wrote: ... I'd like to define a derived type training_class which has flexible no. ... You will have to allocate it for every varialbe of the type ... It turns out the statement of the allocating memory is very long. ...
    (comp.lang.fortran)
  • Strange behaviour with dynamic storage
    ... allocated storage. ... it is possible to call ALLOCATE repeatedly on a component of a ... derived type, ... allocate, STAT = alloc_err) ...
    (comp.lang.fortran)
  • parametrized derived types vs. allocatable components
    ... The size of the array component is given at ... access element ... end program xyz ... I wonder whether one could use parametrized derived type as follows: ...
    (comp.lang.fortran)
  • Re: Initialization for derived-type arrays
    ... also check the source= on the allocate statement. ... Rich Townsend wrote: ... | | Suppose I have a derived type whose components all have default ... If I declare an allocatable array of this type, ...
    (comp.lang.fortran)