array of pointers using integers

Hi,

I am testing different ways for some structures and I want to know if its possible to use the interfaces "molding" for change the arguments, like an array of pointers using an array of integer(kind=C_INT_PTR).

I am trying with this code and with intel compiler work fine, but I'm not sure if is safe or can produce problems in the optimization process (inlining, perfomance, ...)

!-

! x86_64

#define C_INT_PTR 8

#define REAL_KIND 8

module real_pointer

implicit none

public

type real_t

real(REAL_KIND) :: v1, v2

end type real_t

type real_p

type(real_t), pointer :: p

end type real_p

end module real_pointer

!-

program test_pointer

use real_pointer

integer, parameter :: n = 10

integer(C_INT_PTR), dimension(n):: p_array

real(REAL_KIND),dimension(2,n) :: r_array

integer :: i

interface

integer(C_INT_PTR) function MyLoc( value )

real(REAL_KIND), dimension(:), intent(in) :: value

end function MyLoc

subroutine print_pointer_array( n, i_array )

integer, intent(in) :: n

integer(C_INT_PTR), intent(in) :: i_array(n)

end subroutine print_pointer_array

end interface

do i = 1, n

call random_number(r_array(:,i))

end do

do i = 1, n

p_array(i) = MyLoc(r_array(:,i))

end do

call print_pointer_array(n,p_array)

end program test_pointer

!-

integer(C_INT_PTR) function MyLoc( address )

integer(C_INT_PTR), intent(in) :: address

MyLoc = address

end function MyLoc

!-

subroutine print_pointer_array( n, parray )

use real_pointer

integer,intent(in) :: n

type(real_p), dimension(n), intent(in) :: parray

integer :: i

do i = 1, n

print*,parray(i)%p

end do

end subroutine print_pointer_array

!-

Thanks!

[1948 byte] By [clabra01a] at [2007-11-27 9:07:22]
# 1

The code you show is nonstandard.For any given implementation, it might

or might not compile, and if it does compile, it might or might not do what you

expect. It does not compile using Sun f95. Sun f95 notices that the declarations

of MyLoc and print_pointer_array in the interface blocks and in the definitions

have arguments whose types differ, and so it does not compile the program.

Even if it did compile the program, I doubt it would do what you expect.

Bob Corbett

rpcorbetta at 2007-7-12 21:44:00 > top of Java-index,Development Tools,Solaris and Linux Development Tools...
# 2

I know is compiler dependent, but the thing is the possibility of use the interface for casting the variables types of structures.

The warning is because the routines are in the same file. If I use different file with the routines, the warning disappear.

This example is a extreme case, but if is possible to use this "trick" for some reasons. I can change the order in the integer pointer array very fast. In particular, I use a c++ kdtree library who use an array of pointer and make that. The problem is maybe is the performance. I'm not sure about the inlining or the optimization of this kind of things.

clabra01a at 2007-7-12 21:44:00 > top of Java-index,Development Tools,Solaris and Linux Development Tools...
# 3
Why not use the ISO_C_BINDING module? It is defined in the Fortran 2003 standard. Sun's compiler supports it. I don't know which others support it at the moment, but I imagine they all will.
igba at 2007-7-12 21:44:00 > top of Java-index,Development Tools,Solaris and Linux Development Tools...