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
Pointers 派生类型中的关联指针?gFortran vs.英特尔_Pointers_Fortran_Gfortran - Fatal编程技术网

Pointers 派生类型中的关联指针?gFortran vs.英特尔

Pointers 派生类型中的关联指针?gFortran vs.英特尔,pointers,fortran,gfortran,Pointers,Fortran,Gfortran,我想检查派生类型中的指针是否已经定义。我编写了以下简单代码来向您说明我的问题: program test implicit none type y real(8), pointer :: x(:) end type y type(y), pointer :: w(:) allocate(w(2)) allocate(w(1)%x(2)) write(*,*) associated(w(1)%x), associated(w(2)%x) end program test 使用gF

我想检查派生类型中的指针是否已经定义。我编写了以下简单代码来向您说明我的问题:

program test
implicit none

type y
    real(8), pointer :: x(:)
end type y
type(y), pointer :: w(:)

allocate(w(2))
allocate(w(1)%x(2))

write(*,*) associated(w(1)%x), associated(w(2)%x)

end program test
使用gFortran 4.4.1编译此代码并在Ubuntu上运行,结果如下:

T F
而在Windows Vista上使用“英特尔Fortran编译器11.0”编译的相同代码提供:

T T
第一个结果(gFortran)是我实际期望的结果。但是英特尔编译器提供了不同的结果,这让我担心我的代码可能不正确。在这个例子中,我是否对指针做了一些非常错误的事情?有什么想法或解释吗


非常感谢您的帮助

您正在测试一个指针是否在没有显式使用指针上的
null
的情况下关联。关于备注的精彩页面(代码示例已删除):

许多人认为从未关联过的指针的状态是。相关。这是错误的。(…)当一个指针被声明时,它的状态是未定义的,并且不能用
关联的
内在函数安全地查询

看起来gfortran编译器可能被设置为在声明时显式地将指针置零-您可能应该认为这类似于编译器自动将声明的变量设置为零,而不是依赖于该行为。如果你想确定的话,你会自己取消它

编辑

我正在阅读《英特尔编译器指南》,其中指定了如何确保指针正确为空-您可以将派生类型设置为

type y
    real(8), pointer :: x(:) => null()
end type y

但是,请注意,这似乎仅限于Fortran 95,如链接文章中所述。

非常感谢!!这确实是问题所在。您建议的解决方案在英特尔编译器和gFortran上运行良好。