Re: Allocatable components + mixed-language



Steve,
thanks for your example, this will help!

On 13 Jun., 05:52, Steve Lionel <steve.lio...@xxxxxxxxxxxxx> wrote:
Here's an example illustrating this. I note that I erred earlier in
referring to "C_F_PTR" - the procedure is actually C_F_POINTER (I keep
making this mistake). This example shows how a derived type could be
passed from C and the array reconstructed in Fortran using the C Interop
features.

subroutine called_from_c (arg) bind(C)
use, intrinsic :: iso_c_binding
implicit none

! Declare an interoperable derived type
! containing the data address (C pointer)
! and number of elements
type, bind(C) :: argtype
type(c_ptr) :: p
integer(c_int) :: nelements
end type argtype

! Declare the argument as that type
type(argtype), intent(in) :: arg

! Declare a local POINTER to an array of floats.
! We will use this to access the data passed from
! C
real(c_float), pointer, dimension(:) :: x

Can someone think of a way to wrap up those pointers nicely? (there
are currently ~10, although this number may grow a lot larger)
I'd really like to put those into a separate, Fortran-only type, say
"Foo", that I sneak past the C interop somehow; however, passing Foo
to called_from_c is just shifting the problem and will prohibit C
interop. Using a module to make it accessible globally is a Bad Idea
(TM). I'm out of ideas here!

! Convert the array information in the passed argument
! to something we can use.

call c_f_pointer (arg%p, x, [arg%nelements])

! Now x is an array of bounds 1:nelements

While this is cool, won't the pointer assignment have some performance
impact? Our central Fortran routine is called quite often. This is one
of the reasons (besides general tidyness) why I'd like these pointers
wrapped up into a Fortran-only type, so I assign them once and for
good.

--
Steve Lionel
.



Relevant Pages

  • Re: Rules for valid pointer deallocation
    ... Here, you pointer isn't ... the array section array_ref. ... The Fortran standard does not say that the whole array ...
    (comp.lang.fortran)
  • Re: Mixing C++/Fortran using Two-Dimensional Array
    ... not a subset as in Fortran. ... if you really need it you can get the effect by an array of TYPE ... the lowest-level base element pointer, which is all you actually need ... macros in C, although it can be difficult to find macro names that are ...
    (comp.lang.fortran)
  • Re: Fortran and .NET (C#)
    ... pointer is destroyed, just like Fortran ALLOCATABLE. ... > return the address of the first element when returning an array. ...
    (comp.lang.fortran)
  • Re: In-Out Parameters for functions (was: Re: Certified C compilers for safety-critical embedded sys
    ... With the important caveat that there are no array values as such at ... pointer* accesses the array; and declaring a parameter as an array ... declaring an array produces an array). ... particularly help for interfacing from Fortran, ...
    (comp.lang.ada)
  • Re: Indexing of IntPtr
    ... so an interop call returns a pointer to an array which you receive as ... > IntPtr GetArray(); ...
    (microsoft.public.dotnet.languages.csharp)