Re: Allocatable array of allocatable defined types?



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.





.



Relevant Pages

  • Re: Array Fundamentals
    ... When a value-type object is created, C# allocates a single ... the values inside an Array should be allocated ... reside on the heap ... >type is that only the reference type exist on the heap. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Framework 2.0 array redim unsatisfactory performance
    ... I am not talking about "array redim" anymore. ... ReDim simply allocates a new ... but I do not even attempt to test its memory ...
    (microsoft.public.dotnet.languages.vb)
  • bug in debugger?
    ... The code allocates an array of structures: ... and I have a function that allocates the arrays. ... variables on other installations on other machines and have gotten the ... Any advice would be appreciated as I'm about to revert back to VC6. ...
    (microsoft.public.vc.debugger)
  • Re: how to get an allocatable array outside of a subroutine
    ... allocates an array in a subprogram to a size specified in the main program ... Penn State Information Technology Services ...
    (comp.lang.fortran)
  • Re: Memory Lifetime in Interop
    ... large value (so it allocates say a 128 Megabyte array), the memory allocated ... garbage collection when the function returns control to the client) and one ... From watching> the memory heap, if I set dynamic_value in the .NET code to be a rather> large value (so it allocates say a 128 Megabyte array), the memory> allocated ...
    (microsoft.public.dotnet.framework.interop)