Re: Allocatable array of allocatable defined types?
- From: Beliavsky <beliavsky@xxxxxxx>
- Date: 30 Apr 2007 08:00:05 -0700
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.
.
- References:
- Allocatable array of allocatable defined types?
- From: Mark Morss
- Re: Allocatable array of allocatable defined types?
- From: Beliavsky
- Re: Allocatable array of allocatable defined types?
- From: Mark Morss
- Allocatable array of allocatable defined types?
- Prev by Date: Re: Allocatable array of allocatable defined types?
- Next by Date: Re: Fortran DINT help
- Previous by thread: Re: Allocatable array of allocatable defined types?
- Next by thread: Help with Developing a GUI for Fortran program
- Index(es):
Relevant Pages
|
|