我正在尝试将在 C 中初始化的多维数组传递给 FORTRAN 函数并修改数组元素。我在这里发现了一个类似的问题,但我无法让它与多维数组一起工作。尝试写入阵列会导致核心转储。我错过了什么?
代码示例:
#include <stdio.h>
#include <stdlib.h>
double f_function(double ****);
double ****alloc_4D_double(int wlen, int xlen, int ylen, int zlen)
{
int i,j,k;
double ****ary = (double****)malloc(wlen*sizeof(double***));
for (i = 0; i < wlen; i++)
{
ary[i] = (double***)malloc(xlen*sizeof(double**));
for (j = 0; j < xlen; j++)
{
ary[i][j] = (double**)malloc(ylen*sizeof(double*));
for (k = 0; k < ylen; k++)
{
ary[i][j][k] = (double*)malloc(zlen*sizeof(double));
}
}
}
return ary;
}
int main ( void ) {
double ****cary = alloc_4D_double(2, 2, 2, 2);
// intialize values
for (j=0; j < 2; j++)
{
for (k=0; k < 2; k++)
{
for (l=0; l < 2; l++)
{
for (m=0; m < 2; m++)
{
cary[j][k][l][m] = 0;
}
}
}
}
f_function (cary);
return 0;
}
和
real(4) function f_function(cary)
use, intrinsic :: iso_c_binding
!DEC$ ATTRIBUTES C :: f_function
implicit none
real(c_double) , intent(out), dimension(2,2,2,2) :: cary
! attempt to overwrite value (core dump!)
cary=1.1_c_double
...
end function f_function