Re: gfortran and the family of fortran array types
- From: nospam@xxxxxxxxxxxxx (Richard Maine)
- Date: Wed, 27 Aug 2008 01:06:41 -0700
relaxmike <michael.baudin@xxxxxxxxx> wrote:
The fortran 90 array is declared as :
real(kind=C_FLOAT), dimension(:,:), allocatable, target :: mat
To be manageable by the C routine, a conversion must be done so that
only the base_addr field is passed to the C routine.
That is not so. Of course, prior to f2003, the standard didn't provide
any way at all to call C (or have the above C_FLOAT constant), but that
aside, there would be no problems at all with passing the above array to
C. It does *NOT* require the use of C_LOC.
You appear to be assuming that all arrays are inherently passed as dope
vectors, which is not so. I suspect that the confusion partly arises
from looking at argument passing conventions exactly backwards. To see
how an array is passed to a particular subroutine, you do *NOT* start
out by looking at the array in question. You start out by looking at the
interface of the subroutine. That's what defines the expected passing
mechanism.
If the subroutine has an assumed shape, pointer array, or allocatable
array dumy argument, then yes, something like a dope vector is likely to
get passed. But then a C function isn't going to have arguments like
that (unless someone went to a lot of work - and compiler-specific work
- just to make it that way).
If you just properly describe the interface of the C function, the array
will be passed properly, which is to say that just the starting address
will be passed. It does *NOT* matter that this array is allocatable.
That's a common error that people make, even without reference to C.
Just because an actual argument is allocatable, that doesn't mean that
the dummy argument has to be. (If a dummy argument is allocatable, then
th eactual argument must be, but that's a different matter). Once you
have allocated an allocatable array, it is now an array as "good" as any
other and it can be passed as an actual argument to procedures (or C
functions) that don't even need to know that it is allocatable. The same
applies to pointer arrays, I might add, though there is one slight
caveat to that case (involving the possibility that a pointer array
might point to a non-contiguous slice).
The fortran 2003
introduces the C_LOC operator for that purpose :
No. That is not the purpose of C_LOC. C_LOC is not needed at all in this
case. While you could use it, it is just redundant and needlessly
complicated for this case.
In the "Example of C calling Fortran" of the Reid pdf, p37, on the C
side, the array delta is declared with :
double delta[]
On the fortran side, the array is declared as :
REAL (C_DOUBLE),DIMENSION(*),INTENT(IN) :: DELTA
that is, with the fortran 77 syntax which corresponds to a
"assumed-size dummy array".
That's a different scenario from Fortran calling C.
--
Richard Maine | Good judgement comes from experience;
email: last name at domain . net | experience comes from bad judgement.
domain: summertriangle | -- Mark Twain
.
- Follow-Ups:
- Re: gfortran and the family of fortran array types
- From: Jugoslav Dujic
- Re: gfortran and the family of fortran array types
- References:
- Re: gfortran and the family of fortran array types
- From: relaxmike
- Re: gfortran and the family of fortran array types
- From: relaxmike
- Re: gfortran and the family of fortran array types
- Prev by Date: Re: gfortran and the family of fortran array types
- Next by Date: Re: Using INQUIRE to test if a directory exists
- Previous by thread: Re: gfortran and the family of fortran array types
- Next by thread: Re: gfortran and the family of fortran array types
- Index(es):
Relevant Pages
|