C++ calling f90 function returning array

From: ujs (ujs31415_at_yahoo.com)
Date: 01/30/04


Date: 30 Jan 2004 06:27:54 -0800

Hello,
I am trying to call a function (in f90) returning arrays from C++.
How do I do it? (Using Visual C++/Fortran 6.6c).
It would be easier if the f90 was coded as a subroutine vice function, but I
would rather not modify an older routine.
I was able to write an f90 subroutine gateway that called the function and
returned the array in the argument. That worked fine.
But, I would like to call the original function (which was written as a
function to make it a pure function).
Here is a simplified example:

--------------------------
f90 function to be called
--------------------------
module temp ! takes care of the explicit interface
  contains

  function test( x ) result( y )
     real(8), dimension(3) :: x
     real(8), dimension(3) :: y
     y = x
     return
  end function test

end module temp

----------------------------------
C++ caller:
-----------------------------------
#include <iostream>

// Prototype for TEST: note test is actually coded as a subroutine
extern "C" void __stdcall TEST( double* b, double* a );

int main()
{

   double a[3] = {1,2,3};
   double b[3];

   TEST( b, a);
   cout << "b = (" << b[0] << ", " << b[1] << ", " << b[2] << ");" << endl;

   return 0;
}

-----------------------------------
f90 caller:
-----------------------------------
program f90_test
using temp

  real(8) :: c(3) = (/ 1, 2, 3 /)
  real(8) :: d(3)
  d = test(c)
  write(6,*) "d = ", d

end program f90_test

---------------------------------------------------------------
The problem is: (after working out all the linkage problems):
1. I noticed that the function returning array is actually coded like
    "subroutine test( y, x )", hence the declaration in
    C++ as "void( double*, double* )"
2. When extern "C" void __stdcall TEST( double* b, double* a );
 I step into the f90 routine (with the debugger), the values
   of x are correct (i.e., equal to a). However, y has location 0;
   so writing to it causes the program to crash.
3. But, works fine when called from f90. Interestingly, the location of y
   is different from location of d. When it returns from test, however,
   the values for d are correctly set. Is this set by the caller or the
   callee? I don't know what goes on the stack, and who clears stuff from
   the stack.

So, how do I word the C++ declaration?

Thanks for any help!
ujs



Relevant Pages

  • Re: reshaping array in a subroutine
    ... > I am somewhat confused on implicit reshaping of array in Fortran 90. ... > implicit array reshaping in not allowed in the F90 standward. ...
    (comp.lang.fortran)
  • Re: segmentation fault
    ... it would be a good idea to do so as many segmentation faults ... the "random" nature of invalid access to memory. ... belonging to another variable or array) you may only get data corruption ... F90 used as F90 ...
    (comp.lang.fortran)
  • Re: g95 wish list
    ... Perfectly legal in f90 and afterwards. ... to write a zero-sized array constructor. ... but not a scalar. ... Well, in f90, an array element counts as a ...
    (comp.lang.fortran)
  • Re: C++ calling f90 function returning array
    ... | It would be easier if the f90 was coded as a subroutine vice function, ... I noticed that the function returning array is actually coded like ... Callee allocates the space; in this case, the correct prototype is double**. ...
    (comp.lang.fortran)
  • Re: How to allocate variable in f90 subroutine and pass it to c program?
    ... | Thanks Skip Knoble & David Frank for your answers. ... | deals with f90 only, but I am looking for an answer regarding c-language - ... END MODULE Array ... SUBROUTINE GetF90Array(p, nSize) ...
    (comp.lang.fortran)