【发布时间】:2020-09-16 04:50:22
【问题描述】:
我在ISO_C_BINDING 上问了几个问题,我做了一个例行公事。由于我在处理 Fortran POINTER 时总是犯很多错误,所以我想确保没有错误或一些不推荐的奇怪点。
(Fortran 部分)
program test
use iso_c_binding
implicit none
interface
subroutine get_value_array(in, num) bind(C, name='get_value_Array')
use iso_c_binding
implicit none
type(C_PTR), intent(inout) :: in
integer(C_INT), value, intent(in) :: num
end subroutine
end interface
real(C_DOUBLE), allocatable, target :: array(:)
real(C_DOUBLE), pointer :: array_fptr(:)
type(C_PTR) :: array_cptr
integer :: array_len
allocate(array(12))
array_len = size(array,1)
array_cptr = C_LOC(array)
call get_value_array (array_cptr, array_len)
call C_F_POINTER(array_cptr, array_fptr, [array_len])
print *, 'array_fptr'
print *, array_fptr
print *, 'array'
print *, array
end program
(C部分)
void get_value_Array(double **in, int num) {
int i;
for (i = 0; i < num; i++) {
(*in)[i] = i+1;
}
}
(输出)
array_fptr
1.00000000000000 2.00000000000000 3.00000000000000
4.00000000000000 5.00000000000000 6.00000000000000
7.00000000000000 8.00000000000000 9.00000000000000
10.0000000000000 11.0000000000000 12.0000000000000
array
1.00000000000000 2.00000000000000 3.00000000000000
4.00000000000000 5.00000000000000 6.00000000000000
7.00000000000000 8.00000000000000 9.00000000000000
10.0000000000000 11.0000000000000 12.0000000000000
为了确定,这个过程是
-
array_cptr通过C_LOC函数与array相关联。 (也许“关联”在这里不是一个合适的术语,因为它是与 Fortran 指针相关的术语。我应该怎么称呼它?) -
array在我调用get_value_array时被修改,因为子例程(或 C 函数)修改了array_cptr指向的位置。 -
当我打电话给
C_F_POINTER时,array_fptr通过array_cptr与array相关联
最后array_cptr 和array_fptr 是彼此不同的对象(?),同时指向同一个目标array,对吧?
【问题讨论】:
标签: c pointers fortran fortran-iso-c-binding