Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Help straighten me out on c_f_pointer usage

298 views
Skip to first unread message

ksmith

unread,
Aug 17, 2009, 5:10:32 PM8/17/09
to
Hi,

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

Richard Maine

unread,
Aug 17, 2009, 5:23:55 PM8/17/09
to

> I'm using the c_f_pointer subroutine to assocaite a fortran 3D array
> pointer with a C array,

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

mecej4

unread,
Aug 17, 2009, 8:30:28 PM8/17/09
to

"ksmith" <kwms...@gmail.com> wrote in message news:772b21db-e953-4e3e...@b14g2000yqd.googlegroups.com...

> Hi,
>
> 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:
>
<-- CUT CODE -->
>
> 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.
>
The error occurs with

GNU Fortran (GCC) 4.5.0 20090421 (experimental) [trunk revision 146519]

as well.

ksmith

unread,
Aug 17, 2009, 8:53:14 PM8/17/09
to
On Aug 17, 7:30 pm, "mecej4" <mecej4@_no_spam_at_operamail.com> wrote:
> "ksmith" <kwmsm...@gmail.com> wrote in messagenews:772b21db-e953-4e3e...@b14g2000yqd.googlegroups.com...

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

0 new messages