为什么这种将指针从 C++ 传递到 Fortran 的尝试不起作用?

我需要将一个动态数组从 c++ 传递到 Fortran.我做了很多研究来整理一个我认为应该有效的例子,但事实并非如此.该程序应在 c++ 程序中创建一个数组,将该数组的指针传递给 Fortran 例程,将 C 指针转换为 Fortran 指针,然后在 Fortran 端打印该数组.

I need to pass a dynamic array from c++ to Fortran. I did a lot of research to put together an example that I thought should work, but it doesn't. The program should create an array in the c++ program, pass the pointer of that array to the Fortran routine, convert the C pointer to a Fortran pointer, and then print the array on the Fortran side.

我的 c++ 主程序:

My c++ main program:

using namespace std;

extern "C" {
   void cinterface(int*,int*);
}

int main()
{
   int carray[]={0,1,2,3,4};
   int carray_siz=5;

   cinterface(&carray_siz,carray);

   return 0;
}

我的 Fortran 例程:

My Fortran routine:

module fortmod

   use ISO_C_BINDING

   implicit none

contains

   subroutine cinterface(carray_siz,carray_ptr) bind(C)

      implicit none

      integer(c_int), intent(in) :: carray_siz
      type(c_ptr), intent(in) :: carray_ptr

      integer(c_int), pointer :: pfarray(:) => NULL()

      call C_F_POINTER(carray_ptr,pfarray,[carray_siz])

      print *, pfarray

   end subroutine cinterface

end module fortmod

我将其构建为:

gfortran -c fortmod.f90
g++ main.cpp fortmod.o -lgfortran

但是当我运行它时,它没有打印数组值,而是说:

But when I run it, instead of printing the array values, it says:

Segmentation fault (core dumped)

我对指针的概念不熟悉,所以我想我不明白它们是如何正常工作的.您能否指出为什么我在运行此程序时会出现此内存错误?

I'm new to the idea of pointers, so I'm thinking I'm not understanding how they work correctly. Can you please point out why I'm getting this memory error when I run this?

推荐答案

来自gfortran手册:

From the gfortran manual:

如果指针是可互操作过程的伪参数,它通常必须是使用 VALUE 属性声明.void* 匹配 TYPE(C_PTR)、VALUE,而 TYPE(C_PTR) 单独匹配 void**.

If a pointer is a dummy-argument of an interoperable procedure, it usually has to be declared using the VALUE attribute. void* matches TYPE(C_PTR), VALUE, while TYPE(C_PTR) alone matches void**.

我的 Fortran 例程正在寻找指针的地址,而不是指针指向的地址.因此,如果我将 c++ 端修改为:

My Fortran routine was looking for the address of the pointer, not the address to which the pointer is pointing. So if I modify the c++ side to this:

使用命名空间标准;

extern "C" {
   void cinterface(int*,int**);
}

int main()
{
   int carray[]={0,1,2,3,4};
   int carray_siz=5;

   cinterface(&carray_siz,&carray);

   return 0;
}

重新构建并重新运行,我现在得到:

re-build and re-run, I now get:

0     1     2     3     4

正如预期的那样.

相关文章