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.