I'm using the c_f_pointer subroutine to assocaite a fortran 3D array
pointer with a C array, and I'm curious what is *supposed* to happen,
since there's a big difference between g95 (0.92) and gfortran (4.3.3
and 4.2.1) in their behaviour.
Here's the fortran code:
!------------------------------------------------------------------------
subroutine pass_3d(arr, arr_shape) bind(c)
use iso_c_binding
implicit none
type(c_ptr), value :: arr
integer(c_int), intent(in), dimension(3) :: arr_shape
integer(c_int), dimension(:,:,:), pointer :: arr_ptr
call c_f_pointer(arr, arr_ptr, arr_shape)
print *, "arr3D shape: ", shape(arr_ptr)
print *
print *, "arr3D in fortran:", arr_ptr
end subroutine pass_3d
!--------------------------------------------------------------------------------
Here's the C code:
/
*--------------------------------------------------------------------------------
*/
#include <stdlib.h>
#include <stdio.h>
#define N 2
void pass_3d(int *, int[3]);
int main(int argc, char **argv) {
int arr3D[N*N*N];
int arr3D_shape[] = {N,N,N};
int i;
printf("\n\narr3D initialized in C: ");
for(i=0; i<N*N*N; i++) {
arr3D[i] = i;
printf("%d ", arr3D[i]);
}
printf("\n\n");
pass_3d(arr3D, arr3D_shape);
return 0;
}
/
*--------------------------------------------------------------------------------
*/
The output from g95 is:
arr3D initialized in C: 0 1 2 3 4 5 6 7
arr3D shape: 2 2 2
arr3D in fortran: 0 1 2 3 4 5 6 7
Just as I would expect. Nothing funny goes on in the c_f_pointer
call.
But gfortran (4.4.2 and 4.3.3) give this:
arr3D initialized in C: 0 1 2 3 4 5 6 7
arr3D shape: 2 2 2
arr3D in fortran: 0 1 2
3 2 3 4 5
The right number of elements are printed out, but they're not all
there. This is true when I index the array explicitly in gfortran,
too.
What am I doing wrong? Or is this a gfortran bug? If so I'll take it
to their ML.
Thanks,
Kurt
Code elided. Looks fine at least to a casual glance.
> But gfortran (4.4.2 and 4.3.3) give this:
>
> arr3D initialized in C: 0 1 2 3 4 5 6 7
>
> arr3D shape: 2 2 2
>
> arr3D in fortran: 0 1 2
> 3 2 3 4 5
>...
> What am I doing wrong? Or is this a gfortran bug? If so I'll take it
> to their ML.
Looks like a gfortran bug to me. I'd guess even a simple one. Looks like
the stride for the first dimension is wrong I'd guess.
--
Richard Maine | Good judgment comes from experience;
email: last name at domain . net | experience comes from bad judgment.
domain: summertriangle | -- Mark Twain
GNU Fortran (GCC) 4.5.0 20090421 (experimental) [trunk revision 146519]
as well.
For the record, this has a related bug report, and it's getting some
action.
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40962
Thanks to the gfortran maintainers for the response!
Kurt