Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Pointers c_associated()和associated()之间在检查关联方面有什么区别吗?_Pointers_Fortran - Fatal编程技术网

Pointers c_associated()和associated()之间在检查关联方面有什么区别吗?

Pointers c_associated()和associated()之间在检查关联方面有什么区别吗?,pointers,fortran,Pointers,Fortran,在下面的代码中,我试图检查C指针的关联状态(1)使用C_associated()直接指向C指针,或(2)使用associated()指向从C_指针()获得的Fortran指针 在上述情况下,第1行和第2行之间的结果似乎总是相同的。但是,如果cp可能是空指针,我是否应该首先使用c_关联()检查cp的状态,然后仅当cp保证关联(即非空)时才使用c_关联()将(Fortran)指针与c指针的目标关联。C指针必须有一个目标 是的,如果C指针可能未关联,则应检查其关联状态。根据我的知识标准,Fortran

在下面的代码中,我试图检查C指针的关联状态(1)使用
C_associated()
直接指向C指针,或(2)使用
associated()
指向从
C_指针()
获得的Fortran指针


在上述情况下,第1行和第2行之间的结果似乎总是相同的。但是,如果
cp
可能是空指针,我是否应该首先使用
c_关联()
检查
cp
的状态,然后仅当
cp
保证关联(即非空)时才使用
c_关联()
将(Fortran)指针与c指针的目标关联。C指针必须有一个目标


是的,如果C指针可能未关联,则应检查其关联状态。

根据我的知识标准,Fortran要求将C_loc应用于关联指针。因此(如果我是正确的,我不是100%确定,因此有一个注释)上述代码的结果是实现定义的。@IanBush非常感谢,我同意c_loc,我的MWE有点奇怪。。。(守则的意图也不清楚)。因此,我如上所述重新创建了MWE,并希望意图(问题)现在更加明确……我想知道您是希望我“证明”我的答案,还是希望它足够可信,以鼓励相关方查找细节。@francescalus我认为c_f_pointer()只应被赋予非空(关联)是非常合理的C-指针,所以没有“证明”atm也没问题:)谢谢!然后,我将确保首先检查C指针(而不是稍后使用associated())。首先使用
C_associated
没有坏处:在
C_f_指针之后,您知道关联状态(结果是“始终关联”)。
module test_m
    use iso_c_binding
    implicit none

contains

subroutine sub( cp )
    type(c_ptr) :: cp
    integer, pointer :: fp

    print *, "cp ? ", c_associated( cp )   !! Line 1

    call c_f_pointer( cp, fp )

    print *, "fp ? ", associated( fp )   !! Line 2
end subroutine

end module

program main
    use test_m
    implicit none
    type(c_ptr) :: cp
    integer, target :: x

    ! cp = c_loc( x )   !! => T T
    cp = c_null_ptr   !! => F F

    call sub( cp )
end