I roughly have the following situation. I have a C++ function which is called from Fortran code and takes a function pointer and a void pointer as arguments like this
int STDCALL FORTRAN_NAME(CPPFunction, CPPFUNCTION)(
int (*userFunction)(const int *object,
const void *userFunctionUserData),
const void *userData)
{
// ...
int index;
int result;
// ...
result = userFunction(&index, userData);
// ...
}
This is called from Fortran like this
! ...
DOUBLE PRECISION, ALLOCATABLE :: data(:,:)
INTEGER :: n, result
! ...
ALLOCATE(data(3,n)); data = 0.0
! ... fill data with something
result = CPPFUNCTION(FORTRANFUNCTION, data)
! ...
The Fortran function which I want to pass via the function pointer looks like
INTEGER FUNCTION FORTRANFUNCTION(idx, data)
IMPLICIT NONE
INTEGER, INTENT(IN) :: idx
DOUBLE PRECISION, INTENT(IN) :: data(*)
INTEGER :: i, offset
offset = 3 * (idx - 1)
WRITE(*,*) 'data(offset + 1) = ', data(offset + 1)
WRITE(*,*) 'data(offset + 2) = ', data(offset + 2)
WRITE(*,*) 'data(offset + 3) = ', data(offset + 3)
END FUNCTION FORTRANFUNCTION
If I start the whole thing CPPFunction
seems to be correctly called, it calls FORTRANFUNCTION
and I get an exception code
c0000005 ACCESS_VIOLATION
exactly in the line in FORTRANFUNCTION
where the first access to the array data is done.
WRITE(*,*) 'data(offset + 1) = ', data(offset + 1)
Can somebody tell me where my mistake is? I should also mention that the C++-function is not mine. It is not impossible to change it but it would affect lots of other code. The Fortan part is completely under my control and I can do what I want with it.
Thank you.