Re: Allocatable array of allocatable defined types?



On Apr 30, 10:54 am, Mark Morss <mfmo...@xxxxxxx> wrote:
On Apr 30, 10:30 am, Beliavsky <beliav...@xxxxxxx> wrote:

On Apr 30, 9:46 am, Mark Morss <mfmo...@xxxxxxx> wrote:

I want to do something that I rather suspect I can't do in Fortran;
I'm seeking contradiction or confirmation.

I would like to have a defined data type some of whose constituents
are allocatable arrays. The space allocated for a given constituent
would be the same of all instances of the given type. I would then
like to have an allocatable array of these data types.

I don't see the problem. Given an array

type foo
real, allocatable :: y(:)
end type foo

type(foo), allocatable :: x(:)

one could write a subroutine that allocates x and which allocates each
component y to have some size ny.

Components of derived types can be declared PRIVATE, so one could
ensure that the ONLY way the components y get allocated is through the
subroutine.

You could define a derived type

type foo_vec
type(foo), allocatable :: x(:)
integer :: ny
end type foo_vec

where the ny is used to set the size of all the y components of the
x's.

<snip>

One quite inelegant solution has occured to me, which is to define the
constituents of the defined data types with >fixed< size, large enough
to contain the maximum number of elements that I would want to use.
Ugly and wasteful, but perhaps necessary?

You could make x in foo_vec above have a fixed size with little cost,
because unless the components y of the elements of x are ALLOCATED,
little or no memory will be used.

Thanks for those observations. Apparently my confusion is my belief
that if an array is of defined types, each instance of the type found
in the array must have the same size. Yet the following code works
without error when compiled with xlf95:

PROGRAM test
IMPLICIT NONE

TYPE foo
INTEGER :: key
INTEGER,ALLOCATABLE,DIMENSION(:) :: a
END TYPE foo

TYPE(foo),ALLOCATABLE,DIMENSION(:) :: fooarray

INTEGER i,j

ALLOCATE (fooarray(3))
DO i=1,3
ALLOCATE(fooarray(i)%a(i)) ! array of variable size elements
END DO

DO i=1,3
fooarray(i)%key=i
DO j=1,i
fooarray(i)%a(j)=i-j
END DO
WRITE(*,*) fooarray(i)%key,fooarray(i)%a(1:i)
END DO

END PROGRAM test

Why this code works is a mystery to me, since I had thought that
Fortran arrays were highly regular, by which I mean that the space in
a row was a fixed constant for all rows.

That is true for a 2-D array, but an array of derived types, each of
which has an allocatable component, is not the same thing.

.



Relevant Pages

  • 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)
  • Re: output of allocatable array of strings==> blank?
    ... Inside the subroutine, I declare subheader as ... declare the subheader as a BIG_NUMBER of strings array. ... ALLOCATE the array of the right size and pass it, ...
    (comp.lang.fortran)
  • Re: Fortran/C string interop?
    ... You can't allocate them in C. ... recall whether the proposed array API addressed such allocation or not. ... OK, to answer my question: Even though it did not specifically mention characters, I just found in the proposal that the array is valid for any interoperable type: ...
    (comp.lang.fortran)