I have this simple Fortran 90 program:
subroutine apc_wrapper(i, j, k) implicit none integer*8, intent(in) :: i, j integer*8, intent(out) :: k double precision t k = i + js end subroutine
compiled as a shared library
gfortran -O2 -shared -fPIC apc_wrapper.f90 -o apc_wrapper.so
Now I want to call this routine from Julia with all integer arguments, for example,
i = 2 j = 3 k = 0 ccall( (:apc_wrapper_, "./apc_wrapper.so"), Void, (Ptr{Int64}, Ptr{Int64}, Ptr{Int64}), &i, &j, &k)
But that will not work. k will not change its value and continue to estimate 0.
But if I do it
i = 2 j = 3 kk = [0] ccall( (:apc_wrapper_, "./apc_wrapper.so"), Void, (Ptr{Int64}, Ptr{Int64}, Ptr{Int64}), &i, &j, kk)
That is, use an array to store the output, it works! After calling the subroutine, kk evaluates to
1-element Array{Int64,1}: 5
And I didn’t change Fortran code at all, he didn’t even know that he was dealing with an array, just a block of memory.
So, if Fortran is able to read memory blocks ( i and j were correctly red), why can't they write to them?
I have no problem with this. Actually, I want to use an array as output, but still this behavior surprised me.