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

C Interop: Is this OK?

152 views
Skip to first unread message

Ian Bush

unread,
Sep 1, 2011, 6:44:56 AM9/1/11
to

Hi All,

Program gf

Use iso_c_binding

Real( c_double ), Dimension( 1:10 ), Target :: a

Call test( a )

Contains

Subroutine test( aa )

Real( c_double ), Dimension( : ), Target :: aa

Type( c_ptr ), Pointer :: b

b = c_loc( aa( 1 ) )

End Subroutine test

End Program gf

Wot now? nagfor -f2003 -c gf.f90
NAG Fortran Compiler Release 5.2(721)
[NAG Fortran Compiler normal termination]
Wot now? g95 -std=f2003 -c gf.f90
Wot now? ifort -stand=f03 gf.f90
Wot now? f90 gf.f90
Wot now? ~/Downloads/open64-4.2.4/bin/openf95 gf.f90
Wot now? gfortran -c -std=f2003 gf.f90
gf.f90:17.15:

b = c_loc( aa( 1 ) )
1
Error: Assumed-shape array 'aa' at (1) cannot be an argument to the
procedure 'c_loc' because it is not C interoperable
Wot now?

(f90 is the sun or whatever it is called now compiler)

So is gfortran correct? Version 4.5.2 if it is of interest,

Ian

Ian Harvey

unread,
Sep 1, 2011, 9:51:31 AM9/1/11
to
On 1/09/2011 8:44 PM, Ian Bush wrote:
...

> Subroutine test( aa )
> Real( c_double ), Dimension( : ), Target :: aa
> Type( c_ptr ), Pointer :: b
> b = c_loc( aa( 1 ) )
> End Subroutine test
>
> End Program gf
>
...

> Wot now? gfortran -c -std=f2003 gf.f90
> gf.f90:17.15:
>
> b = c_loc( aa( 1 ) )
> 1
> Error: Assumed-shape array 'aa' at (1) cannot be an argument to the
> procedure 'c_loc' because it is not C interoperable
...
Compiler bug - the argument to c_loc in your example program isn't an
array, it is a scalar (and that scalar is interoperable and meets the
other requirements for an argument to c_loc).

glen herrmannsfeldt

unread,
Sep 1, 2011, 2:19:49 PM9/1/11
to
Ian Harvey <ian_h...@bigpond.com> wrote:

(snip)


>> b = c_loc( aa( 1 ) )
>> 1
>> Error: Assumed-shape array 'aa' at (1) cannot be an argument to the
>> procedure 'c_loc' because it is not C interoperable

> Compiler bug - the argument to c_loc in your example program isn't an

> array, it is a scalar (and that scalar is interoperable and meets the
> other requirements for an argument to c_loc).

Does look like a compiler bug to me, too.

Note that in C, a pointer to an array is equivalent to a pointer
to its first element. (Or is it the other way around.)

That isn't true in Fortran, and the compiler shouldn't assume
that it is. (That is, assume that you meant c_loc(aa).)

If the actual argument is contiguous, then you should be able to
use the pointer in C to address the whole array. If it isn't
contiguous, then you can't do that.

Did you try a Fortran pointer pointing to a(1), and then as an
argument to c_loc()?

-- glen

Ian Bush

unread,
Sep 2, 2011, 3:26:06 AM9/2/11
to

Thanks, that's what I thought. I'll report it in a bit,

Ian

Ian Bush

unread,
Sep 2, 2011, 3:30:19 AM9/2/11
to
On 01/09/11 19:19, glen herrmannsfeldt wrote:
> Ian Harvey<ian_h...@bigpond.com> wrote:
>
> (snip)
>>> b = c_loc( aa( 1 ) )
>>> 1
>>> Error: Assumed-shape array 'aa' at (1) cannot be an argument to the
>>> procedure 'c_loc' because it is not C interoperable
>
>> Compiler bug - the argument to c_loc in your example program isn't an
>> array, it is a scalar (and that scalar is interoperable and meets the
>> other requirements for an argument to c_loc).
>
> Does look like a compiler bug to me, too.
>
> Note that in C, a pointer to an array is equivalent to a pointer
> to its first element. (Or is it the other way around.)
>
> That isn't true in Fortran, and the compiler shouldn't assume
> that it is. (That is, assume that you meant c_loc(aa).)

Yup, indeed. This is ultimately the reason I'm doing this.

>
> If the actual argument is contiguous, then you should be able to
> use the pointer in C to address the whole array. If it isn't
> contiguous, then you can't do that.
>
> Did you try a Fortran pointer pointing to a(1), and then as an
> argument to c_loc()?
>

Good idea! Why didn't I think of that ...

It works, ta muchly!

Ian

Ian Bush

unread,
Sep 2, 2011, 5:31:41 AM9/2/11
to

Thanks Tobias!

Ian

0 new messages