- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试将一个结构从 fortran 传递到 C,其中 Fortran 中的结构有一个可分配的。我想在 fortran 中的结构中分配数组并在 C 中读取它。但是,当我尝试在 C 中打印分配的数组时,我收到错误消息段错误(核心转储)。我正在使用英特尔编译器版本 19.0.1.144。
我对 C 比较陌生,所以为了测试这个想法,我在 fortran 中分配了一个一维数组并将它传递给 C。这很好用。您可以在 fortran 代码中看到 ff 是可分配的数组,并且它被分配了一个维度 ii(等于 5)。我初始化数组 ff 并使用类型(C_ptr)的指针来存储 ff 数组的地址。我在 C 中打印 ff,它打印出正确的值 ff,即 14.7、15.7、16.7、17.7、18.7。使用相同的想法,我已经声明了一个结构体 data1,其中包含一个整数 ival、一个实数 cval 和一个可分配变量 dval。我正在为结构类型 data1 分配一个指针 pdata。我分配了指针和数组 dval。我在 C Main 中调用 fortran 来打印结构体数量,但在打印数组 d(或 dval)时出现错误。此外,C 中 ival 和 cval 的值的值是不正确的。
SUBROUTINE Simulation(ii,ffp,cdata) BIND(C)
use, intrinsic :: iso_c_binding
integer(kind=4), intent(in) :: ii
type(C_PTR), intent(out) :: ffp
real (C_double), dimension(:), allocatable, target, save :: ff
integer(kind=4) :: err, i
Type :: data1
Integer :: ival
Real :: cval
Real (C_double), dimension(:), allocatable :: dval
end Type data1
type(C_ptr), intent(out) :: cdata
type(data1), Target, save :: tdata
type(data1), pointer :: pdata
!Allocating memory for pdata:
Allocate(pdata,stat=err)
print *,'allocate returned: ',err
!Allocating memory for dval:
allocate(pdata%dval(ii),stat=err)
print *,'allocate returned: ',err
!Allocating memory for ff:
allocate(ff(ii),stat=err)
print *,'allocate returned: ',err
ffp = C_LOC(ff(1))
cdata = C_Loc(pdata)
pdata%ival = 4
pdata%cval = 17.6
pdata%dval(1) = 1.2
pdata%dval(2) = 1.2*2
pdata%dval(3) = 1.2*3
pdata%dval(4) = 1.2*4
pdata%dval(5) = 1.2*5
print*,"ival = ",pdata%ival
print*,"cval = ",pdata%cval
print*,"davl(1) = ",pdata%dval(1)
print*,"davl(4) = ",pdata%dval(4)
write(*,*) '#Fortran address of ff:', LOC(ff(1))
write(*,*) '#Fortran address: pdata', LOC(pdata)
write(*,*) 'size of ff =',sizeof(ff)
write(*,*) 'size of pdata =',sizeof(pdata)
do i=1,ii
ff(i) = i + 13.7d0
end do
END SUBROUTINE SIMULATION
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
struct temp {int ival; float cval; double *d;};
void simulation(int *ii, double **ff, struct temp *data2);
int main()
{
int ii = 5;
double *ff;
struct temp data2;
int i;
simulation(&ii,&ff,&data2);
printf("#C address of ff: %d\n", ff);
printf("#C address of data2: %d\n", data2);
printf("Size of ff in C is %d\n", sizeof(ff));
for (i=0; i<ii; i++)
printf("ff[%d] = %f\n",i,ff[i]);
printf("data ival %d \n",data2.ival);
printf("data cval %f \n",data2.cval);
printf("data dval(1) %f \n",data2.d[0]);
printf("data dval(4) %f \n",data2.d[3]);
return 0;
}
bash-4.2$ ./a.out
allocate returned: 0
allocate returned: 0
allocate returned: 0
ival = 4
cval = 17.60000
davl(1) = 1.20000004768372
davl(4) = 4.80000019073486
#Fortran address of ff: 26650144
#Fortran address: pdata 26636320
#Fortran address: tdata 7054304
size of ff = 40
size of pdata = 80
size of tdata = 80
#C address of ff: 26650144
#C address of data2: 26636320
Size of ff in C is 8
ff[0] = 14.700000
ff[1] = 15.700000
ff[2] = 16.700000
ff[3] = 17.700000
ff[4] = 18.700000
data ival 26636320
data cval 0.000000
Segmentation fault (core dumped)
最佳答案
派生类型 data1
不是 C 互操作的:它有一个可分配的组件。 intent(out)
虚拟参数 cdata
分配结果 C_LOC(pdata)
哪里pdata
属于这种不可互操作的类型。C_LOC
当该参数不可互操作时,向实际参数返回一个“不透明句柄”。这意味着(Fortran 2018 18.2.3.6):
Where the actual argument is of noninteroperable type or type parameters, the result of C_LOC provides an opaque “handle” for it. In an actual implementation, this handle might be the C address of the argument; however, only a C function that treats it as a void (generic) C pointer that cannot be dereferenced (ISO/IEC 9899:2011, 6.5.3.2) is likely to be portable.
data2
在 C 主程序中进行这种不可移植的解引用。 .从根本上说,可分配的 Fortran 数组与 C 取消引用的指针不同。
data2
回到 Fortran 程序进行进一步处理。当虚拟参数是可分配数组时,Fortran 过程甚至可以互操作。
关于c - Fortran/C 互操作性将结构从 Fortran 传递到带有可分配项的 C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58397095/
我是一名优秀的程序员,十分优秀!