@giraffe ,
With the C and Fortran exampIe in the previous post, note a few points:
- The
INTERFACEsetting in Fortran can be as follows:
interface
function func(dest, src) result(r) bind(C, name="func_")
! C prototype as follows
! typedef struct my_data2 {
! int value;
! } DAT;
!
! int func_(DAT **, DAT *)
import :: c_int, c_ptr
! Argument list
type(c_ptr), intent(in) :: dest(*)
type(c_ptr), intent(in), value :: src
! Function result
integer(c_int) :: r
end function
end interface
- Note the call on the Fortran side:
..
addr = transfer( source=THE_ADDR(1+N*2), mold=addr )
i = func( dest=[addr], src=c_loc(foo) )
..
So if you cannot follow earlier advice but you need to work with integer types for memory addresses, then try to do as advised by @everythingfunctional which is no pointer arithmetic in Fortran. Instead, locate the memory addresses (the above example does so by calls to C_LOC on the source data) and place in an integer type of kind c_intptr_t and do the TRANSFER operation to “cast” it to type(c_ptr) prior to invoking the C function in a rank-1 object. So note again the line
i = func( dest=[addr], src=c_loc(foo) )
Now if you replace the c_intptr_t with c_int, you will find it work on 32-bit systems but segfault on 64-bit ones. Once you start to track this aspect in your employer code, you will find how to resolve the issues. Note of course, this is only based on the information you have shared thus far.
P.S.> In all of above, I presume you have a use case to work with rank-2 objects of your mydata_2 struct and that is why you have the pointer to pointer (mydata_2 **) parameter in the C function. And that is why the above examples use rank-2 objects.