Neil Carlson asks:
>Will this work with 0-sized actual arguments? Of course I'm assuming
>that the C pointer will never be de-referenced in this case. Basically,
>I'm wondering if some sensibly-valued address gets passed.
I don't understand. If you don't use the address, and don't deref it, why does
its value matter?
(In other words, someone must be using the address or its value would not
matter!)
>And more specifically, does C_LOC return some sensible dummy value for a
>0-sized array argument, again with the caveat that you shouldn't attempt to use
>it for anything.
The standard explicitly states
"If it is an array, it shall be contiguous and have nonzero size."
precisely because there is no good answer for C_LOC to give.
But the example code you show does not use type C_PTR, just a normal array.
Again, the standard explicitly rules this out
"An ... assumed-size array ... is interoperable with a C array if its size is
nonzero and ..."
This is, IIRC, because C99 does not have zero-sized arrays, so there is nothing
available for a Fortran zero-sized array to interoperate with.
So you are at best in the realm of compiler extensions.
I note that if the dummy argument is type C_PTR, i.e. you change your example to
interface
subroutine func (array, ...) bind(c)
type(c_ptr),value :: array
and only use C_LOC if the actual argument is nonzero-sized, there is no problem.
In the zero-sized case you might want to set the argument to pass to C_NULL_PTR,
but even if you pass an undefined C_PTR value, that won't present a problem
unless you try to use it.
I note that the "Further C Interoperability" TS, which will be incorporated into
the next revision of the standard, has facilities for interoperating with
assumed-shape arrays (as well as other kinds of dummy array), and in this case
zero size would not be a problem. However, most compilers are yet to implement
this TS so it is not a solution right now (but will be in a few years time when
the compilers catch up). Even with the TS, an explicit-shape or assumed-size
dummy cannot itself interoperate unless it is nonzero-sized, but that can be
worked around fairly easily.
Cheers,
--
................................Malcolm Cohen, Nihon NAG, Tokyo.
|