getting offset of allocatable derived types
- From: hatingspam2009@xxxxxxxxx
- Date: Mon, 5 Jan 2009 14:02:37 -0800 (PST)
I have the following test code that returns the offset of a derived
type that has an allocatable array as one of its components. It
returns 48, 72, and 64 for gfortran, ifort and g95 respectively. If I
make the "sensors" array non-allocatable then it returns the expected
result of 8 (assuming integer*4) for all the mentioned compilers. Can
I not use the C_LOC function and get the correct pointer address when
a structure contains an allocatable array. If not, then is there some
portable way of doing it.
PROGRAM main
USE, INTRINSIC :: ISO_C_BINDING
IMPLICIT NONE
TYPE vehicle_t1
INTEGER(C_INT), DIMENSION(:), ALLOCATABLE :: sensors
END TYPE vehicle_t1
TYPE(vehicle_t1), DIMENSION(1:2), TARGET :: walloc
TYPE(C_PTR) :: f_ptr3, f_ptr4
INTERFACE
INTEGER(C_INTPTR_T) FUNCTION h5offsetof(start,end) RESULT(offset)
USE, INTRINSIC :: ISO_C_BINDING
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: start, end
END FUNCTION h5offsetof
END INTERFACE
ALLOCATE(walloc(1)%sensors(1:2))
ALLOCATE(walloc(2)%sensors(1:2))
f_ptr3 = C_LOC(walloc(1))
f_ptr4 = C_LOC(walloc(2))
! does not work either
! f_ptr3 = C_LOC(walloc(1)%sensors(1))
! f_ptr4 = C_LOC(walloc(2)%sensors(1))
PRINT*,'H5OFFSETOF', H5OFFSETOF(f_ptr3,f_ptr4)
END PROGRAM main
INTEGER(C_INTPTR_T) FUNCTION h5offsetof(start,end) RESULT(offset)
USE, INTRINSIC :: ISO_C_BINDING
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: start, end
INTEGER(C_INTPTR_T) :: address_start, address_end
address_start = TRANSFER(start, address_start)
address_end = TRANSFER(end , address_end )
offset = address_end - address_start
END FUNCTION h5offsetof
.
- Follow-Ups:
- Re: getting offset of allocatable derived types
- From: Craig Powers
- Re: getting offset of allocatable derived types
- From: Richard Maine
- Re: getting offset of allocatable derived types
- Prev by Date: Re: input (was: Autojen m?r? Kiinassa/Usa: ssa)
- Next by Date: Re: What is the value of pi?
- Previous by thread: Re: FORTRAN dynamic arrays used as parameters of functions called from C++
- Next by thread: Re: getting offset of allocatable derived types
- Index(es):