From: pehache-tolai on
Hello,

In my code I need to determine the size of various variables which are
defined as derived types.

Until now I used to use the INQUIRE() function, but it doesn't work if
the derived type contains some pointers. Is there a (not too
complicated) solution ? I don't need to know the size of the objects
pointed or allocated by the pointers, but only the size of the derived
type.

Thanks,

--
pehache
From: robert.corbett on
On Mar 25, 12:49 am, pehache-tolai <pehach...(a)gmail.com> wrote:
> Hello,
>
> In my code I need to determine the size of various variables which are
> defined as derived types.
>
> Until now I used to use the INQUIRE() function, but it doesn't work if
> the derived type contains some pointers. Is there a (not too
> complicated) solution ? I don't need to know the size of the objects
> pointed or allocated by the pointers, but only the size of the derived
> type.
>
> Thanks,
>
> --
> pehache


The easiest way would be to declare an array of two of them
and subtract the LOC of the second from the LOC of the first.
For example, using Oracle Solaris Fortran in 32-bit mode, the
program

PROGRAM MAIN
TYPE T
TYPE(T), POINTER :: P
END TYPE
TYPE(T) A(2)
PRINT *, LOC(A(2)) - LOC(A(1))
END

prints

4

The function LOC is a nonstandard extension, but one that
is almost always present. I showed in a previous posting
how to do the same thing using only standard features,
but some people thought that approach was too ugly to even
consider.

Bob Corbett
From: pehache-tolai on
On 25 mar, 10:58, robert.corb...(a)sun.com wrote:
>
> The easiest way would be to declare an array of two of them
> and subtract the LOC of the second from the LOC of the first.
> For example, using Oracle Solaris Fortran in 32-bit mode, the
> program
>
>       PROGRAM MAIN
>         TYPE T
>           TYPE(T), POINTER :: P
>         END TYPE
>         TYPE(T) A(2)
>         PRINT *, LOC(A(2)) - LOC(A(1))
>       END
>
> prints
>
>  4
>
> The function LOC is a nonstandard extension, but one that
> is almost always present.  

Thanks !

Now I remember that I already seen something like that some years
ago...

> I showed in a previous posting
> how to do the same thing using only standard features,
> but some people thought that approach was too ugly to even
> consider.
>

Probably I would have tried to equivalence a variable of the derived
type with a large enough integer array, then fill the derived type
with some values, then check until which element the integer array has
been modified. But I guess this can fail in some cases :

type my_type
real :: x
complex, pointer :: y(:)
integer :: n
end type
type(my_type) :: A
integer :: B(1000) ! B must be larger than A
equivalence (A,B)

B(:) = 0
A%x = huge(0.0)
allocate( A%y(10) )
A%n = huge(0)
size_of_my_type = size(B)
while (B(size_of_my_type) == 0 .and. size_of_my_type > 0)
size_of_my_type = size_of_my_type - 1
end while


--
pehache

From: Richard Maine on
pehache-tolai <pehache.7(a)gmail.com> wrote:

> In my code I need to determine the size of various variables which are
> defined as derived types.
>
> Until now I used to use the INQUIRE() function, but it doesn't work if
> the derived type contains some pointers. Is there a (not too
> complicated) solution ? I don't need to know the size of the objects
> pointed or allocated by the pointers, but only the size of the derived
> type.

There is currently no portable standard-conforming way.

Bob mentioned the LOC function, but I think he overstates when he says
it is "almost always present." I haven't actually checked recently, but
I doubt it is present in some of the compilers I have most used. I'd
probably agree if the claim were modified to say that almost all
compilers have some similar functionality, possibly under a different
name.

One possible variant that you will probably find in most current
compilers is the f2003 C_LOC function. It does require extra trickery to
convert the results of C_LOC into integers for subtraction. One way
would be to do it in a short C function.

The equivalence trick you mentioned involves a nonstandard equivalence
that some compilers will reject.

As long as you are looking at nonstandard solutions anyway, you might
look at one that is at least slated to become standard. I think that
storage_size is in the f2008 draft. I proposed it for f2003, as a
substitute for what I considered to be anacronisms otherwise being added
to f2003, but my proposal didn't make it. (F2003 added ways to find
storage sizes, as long as you were working only with f77 types. I
considered that an anacronism. For example, you could not even get the
sizes of intrinsic types of other than default or double precision
kinds).

--
Richard Maine | Good judgment comes from experience;
email: last name at domain . net | experience comes from bad judgment.
domain: summertriangle | -- Mark Twain
From: glen herrmannsfeldt on
robert.corbett(a)sun.com wrote:
(snip)

> The easiest way would be to declare an array of two of them
> and subtract the LOC of the second from the LOC of the first.

As I found out when I tried, though, you can't subtract
C_LOC(first) from C_LOC(second). It would be nice to
be able to do that.

-- glen