I have a following code in Fortran and with C "master". It gives sigsegv, but I do not understand why. It is supposed to take a pointer from the master C program convert it to Fortran pointer format, and read out the data C pointer was pointing to. Nothing fancy, but still a SigSegv. This is BTW the only way I see to circumvent lack of interoperability of Fortran multidimensional arrays with... (yeah, C does not have multidimensional arrays).
The Fortran part:
module ret
implicit none
integer, pointer :: a(:,:)
end module ret
module func
implicit none
contains
subroutine initialize(cp) bind(c,name='initialize')
use ret
use iso_c_binding
implicit none
type(c_ptr) :: cp
call c_f_pointer(cp,a,[5,5])
end subroutine initialize
subroutine printa bind(c,name='printa')
use ret
implicit none
integer :: i,j
do i=1,5
do j=1,5
print *,i,j,a(i,j)
end do
end do
end subroutine printa
end module func
The C Part:
#include<stdio.h>
void initialize( void *);
void printa();
int main()
{
int *tab;
tab = (int *)malloc(25 * sizeof (int *));
int i;
for(i=0;i<25;++i)
tab[i]=i;
initialize(tab);
printa();
printf("ok\n");
return 0;
}
The SIGSEGV occurs at line print *,i,j,a(i,j)
in the Fortran part
EDIT:
I have changed "integer*8" back into "integer"
and do i=1,10
back into do i=1,5
. For this version the quesion is still valid
SIGSEGV appears for i=1, and j=1
I am adding this as an answer just in case someone else comes here with a problem, similar to yours.
There are two problems in the code shown. First, you are allocating memory for integer pointers as noted by Klas Lindbäck. This is not the source of the problem since pointers are at least the size of int
or wider than that, but nevertheless the correct data type should be provided to the sizeof
operator in order to prevent overuse of the resources available.
The second problem is that in Fortran arguments are passed by reference. This means that when one calls Fortran routines from C, then the address-of operator &
should be used. Also constant should be placed in separate constant variables and also passed by address. With the previous problem corrected, the C code should be:
tab = malloc(25 * sizeof (int)); // int instead of int *
int i;
for(i = 0; i < 25; ++i)
tab[i] = i;
initialize(&tab); // pass tab by address
Fortran 2003 introduced the VALUE
attribute that can be applied to dummy routine arguments in order to make them pass-by-value instead. If your Fortran compiler implements this F2003 feature, then you might change the declaration of the cp
dummy argument of initialize
to read:
type(c_ptr), value :: cp
If you do this, you would not need to pass tab
by reference.