(j3.2006) Should character strings be interoperable with char arrays?

Aleksandar Donev donev1
Wed Jul 25 13:25:45 EDT 2007


Jim,

> So this is a fundamental difference and I can't see how "pass-by-address"
> calling convention can help you in this case.
You have had the luck of not being on the committee long enough to know my bag 
of complaints. Let me enlighten you :-)

The issue is not with the fact that we must use some special method for 
dealing with C concepts that do not directly translate into Fortran, such as 
void pointers or arrays of arrays (really arrays of pointers), or the 
difference between C and Fortrans strings. The issue is whether TYPE(C_PTR) 
and C_LOC provide a *good* solution. C_LOC has two, IMO, huge problems. It 
requires the TARGET attribute, and contiguity (this was worse in F2003 since 
we did not have a concept of contiguity). Neither of these are needed when 
passing an array to C so long as the C argument is declared with the same 
type as the actual (so no void* arguments!) and there is not an array of 
pointers argument. Sadly these two (especially void*) happen often in 
realistic C libraries.

The reason we require TARGET is because C may keep the pointer around. In such 
cases of course TARGET should be required. But, in most cases of practical 
interest, this is not the intention. One simply wants to pass the array to C 
and then go on with life, just as with normal argument passing.

Similarly with contiguity. The best is what we have in Fortran already: If the 
array is not contiguous, a copy in/out should be made, and the copy thrown 
away after the call completes.

Whether it is possible to find a better solution is a separate issue. I think 
so. But the original designers of Interop (before I joined the committee), as 
I was told, wanted to keep interop to what is a direct correspondence between 
C and Fortran concepts. Anything that does not directly correspond is simply 
"not interoperable" and shoved to users to deal with. For example, users are 
expected to add C_CHAR_NULL to strings themselves...we couldn't be bothered 
to assist them in writing correct programs (I almost always forget the 
padding at first and get a segfault after which I fix it). I think that is a 
bad design...we should be *helping* users write multi-language programs, not 
just *allowing* them to.

Best,
Aleks



More information about the J3 mailing list