I have about the following situation. I have a C ++ function that 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) {
It 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 that I want to pass using 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 everything that CPPFunction seems to be correctly called, it calls FORTRANFUNCTION, and I get the exception code c0000005 ACCESS_VIOLATION exactly on the line in FORTRANFUNCTION, where the array is first accessed.
WRITE(*,*) 'data(offset + 1) = ', data(offset + 1)
Can someone tell me where is my mistake? I should also mention that C ++ is not my function. It is not possible to change it, but it will affect many other codes. Part of Fortan is completely under my control, and I can do what I want with her.
Thanks.