Hello,
I am trying to connect some C code to some Fortran code without rewriting large portions of either. The fortran code has several derived type definitions which are apparently NOT interoperable with C struct definitions (the derived types contain fortran pointer components). Changing the derived type defintions is NOT a particularly attractive option for us at this point so I was hoping to be able to treat the derived type allocations as opaque memory pointer handles in C and rely on fortran routines alone to manipulate the fortran data structures.
Anyhow, I am trying to get my head wrapped around the differences between a C pointer and a Fortran pointer. Reading up on the ISO_C_BINDING info I thought I understood that the C_LOC function will return the 'C' compatible address of a fortran data structure given some limitations on the fortran data types being used. With that in mind, the following fortran code is supposed to allocate some memory for a fortran derived data type called 'SomeDerivedType' and return the 'C' compatible address of that memory back to the calling C function:
function makeDerivedType() bind (c, name='makeDerivedType') use iso_c_binding type (C_PTR) :: makeDerivedType type (SomeDerivedType), pointer :: sdt allocate(sdt) makeDerivedType = C_LOC(sdt) end subroutine
When I invoke this function I get a segfault during the call to C_LOC(sdt) instead.
I must be misunderstanding what C_LOC does. I assumed it would just return an opaque C pointer to the memory used by the fortran allocated data structure so that I could pass that pointer back and forth between the C and Fortran code at will so long as the Fortran code did all the internal manipulation of the data.
Anyone able to shed some light on my misunderstanding? It would be greatly appreciated.
-Travis