将动态2D数组从c++传递到Fortran并返回

时间:2021-10-11 13:12:40

Passing a fixed 2D array between C++ and Fortran works fine, however not so with the program I have written to pass a 2D dynamic array from C++ to Fortran.

在c++和Fortran之间传递一个固定的2D数组是可行的,但是在我编写的程序中却不行,它将一个2D动态数组从c++传递到Fortran。

C++ side

c++的一面

extern "C" {void array2d_(double **, int *, int *); }
using namespace std;
int main()
{
double **array;
int nx=3;
int ny=2;
int i,j;
cout << "Passing dynamic array from C to Fortran\n";
array = (double **) malloc(nx * sizeof(double *));
if(array == NULL)
        {
        fprintf(stderr, "out of memory\n");
        exit;
        }
for(i = 0; i < nx; i++)
        {
        array[i] = (double *) malloc(ny * sizeof(double));
        if(array[i] == NULL)
            {
            fprintf(stderr, "out of memory\n");
            exit;
            }
        }
for(i = 0; i < nx; i++)
            {
            for(j = 0; j < ny; j++)
                {
                array[i][j]=i+j+i*(2+j)+4;  //random initialisation
                cout << "array[" << i << "][" << j << "]=" << array[i][j] << " ";
                }
            cout << endl;
            }

array2d_(array, &nx, &ny);

for(i = 0; i < nx; i++)
        free(array[i]);
    free(array);
return 0;
}

The fortran side

fortran的一面

subroutine array2d(arr,nx_C,ny_C) bind(C,name="array2d_")
use  iso_c_binding
implicit none
integer (C_INT), intent(IN) :: nx_C,ny_C          !array sizes from C
real (C_DOUBLE), intent(INOUT) :: arr(ny_C,nx_C)
integer :: k,l
print *, "This is in Fortran routine..."
do k = 1,ny_C
do l=1,nx_C
 print *, "arr(",k,",",l,") = ", arr(k,l)
end do
end do
end subroutine array2d

The output in C++ is

c++中的输出是

 array[0][0]=4 array[0][1]=5 
 array[1][0]=7 array[1][1]=9 
 array[2][0]=10 array[2][1]=13 

While in Fortran the output is

而在Fortran中,输出是

 arr(           1 ,           1 ) =    1.7994937190948764E-305
 arr(           1 ,           2 ) =    7.1027035167764720E-251
 arr(           1 ,           3 ) =    9.8813129168249309E-324
 arr(           2 ,           1 ) =    5.4809152658772852E-317
 arr(           2 ,           2 ) =    1.5475240269406953E-314
 arr(           2 ,           3 ) =    0.0000000000000000  

So somehow the values are not passed correctly.

所以值传递不正确。

1 个解决方案

#1


5  

The main reason is that your C array is a jagged array, it is an array of pointers to separate 1D arrays while in Fortran you declare ther argument to be a contiguous 2D array. You must use the same in both parts, preferably use a contiguous array in C too.

主要的原因是你的C数组是一个不规则的数组,它是一个用来分离1D数组的指针数组,而在Fortran中你把这个参数声明为一个连续的2D数组。您必须在这两部分中使用相同的方法,最好也使用C中的连续数组。

Just malloc one big nx*ny buffer and set the pointers to the rows instead of alloacating them. You can see an example in https://*.com/a/5901671/721644

只需malloc一个大的nx*ny缓冲区,将指针设置为行,而不是分配它们。您可以在https://*.com/a/5901671/721644中看到一个示例

#1


5  

The main reason is that your C array is a jagged array, it is an array of pointers to separate 1D arrays while in Fortran you declare ther argument to be a contiguous 2D array. You must use the same in both parts, preferably use a contiguous array in C too.

主要的原因是你的C数组是一个不规则的数组,它是一个用来分离1D数组的指针数组,而在Fortran中你把这个参数声明为一个连续的2D数组。您必须在这两部分中使用相同的方法,最好也使用C中的连续数组。

Just malloc one big nx*ny buffer and set the pointers to the rows instead of alloacating them. You can see an example in https://*.com/a/5901671/721644

只需malloc一个大的nx*ny缓冲区,将指针设置为行,而不是分配它们。您可以在https://*.com/a/5901671/721644中看到一个示例