派生类型中的关联指针? gFortran 与英特尔

发布于 2024-08-15 03:27:44 字数 565 浏览 3 评论 0原文

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

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

而使用 Intel Fortran 编译器 11.0 在 Windows Vista 上编译的相同代码提供:

T T

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

非常感谢您的帮助!

I would like to check if a pointer inside a derived type has already been defined or not. I wrote the following simple code to show you my problem:

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

Compiling this code with gFortran 4.4.1 and running it on Ubuntu gives the result:

T F

whereas the same code compiled on Windows Vista with the Intel Fortran compiler 11.0 provides:

T T

The first result (gFortran) is what I am actually expecting. But the fact that the Intel compiler provides a different result makes me fear my code might not be correct. Am I doing something terribly wrong with the pointers in this example? Any idea or explanation?

Many thanks in advance for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南烟 2024-08-22 03:27:44

您正在测试指针是否已关联,而无需在指针上显式使用 nullify。关于 常见 Fortran 错误 注释的精彩页面(带有代码样本已移除):

很多人认为从未关联过的指针的状态是.not。联系。这是错误的。 (...) 当声明指针时,其状态未定义,并且无法使用关联内在函数安全地查询。

看起来 gfortran 编译器可能被设置为显式地使声明时的指针无效 - 您可能应该将其视为编译器自动将声明的变量设置为零,而不是指望这种行为。如果你想确定的话,你自己就将其作废。

编辑

我正在阅读英特尔编译器指南,它指定了如何确保指针正确无效 - 您可以将派生类型设置为“

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

注意”,但是,它看起来像这样仅限于 Fortran 95,如链接文章中所述。

You are testing to see if a pointer is associated without explicitly using nullify on the pointers. A great page on common Fortran mistakes remarks (with the code sample removed):

Many people think that the status of a pointer which has never been associated is .not. associated. This is false. (...) When a pointer is declared its status is undefined, and cannot be safely queried with the associated intrinsic.

It looks like the gfortran compiler may be set up to explicitly nullify pointers on declaration - you should probably think of this like the compiler automatically setting declared variables to zero, and not count on that behavior. If you want to be sure, you will nullify it yourself.

Edit:

I'm reading through the Intel compiler guide, and it specifies how to make sure that the pointer is nullified correctly - you can set up your derived type as

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

Note, however, that it seems like this is limited to Fortran 95, as mentioned in the linked article.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文