派生类型中的关联指针? gFortran与Intel [英] Associated pointers in derived type? gFortran vs. Intel

查看:123
本文介绍了派生类型中的关联指针? gFortran与Intel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

使用gFortran 4.4.1编译此代码并在Ubuntu上运行可得到结果:

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

T F

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

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

T T

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

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?

在此先感谢您的帮助!

推荐答案

您正在测试以查看是否已关联指针,而未在指针上显式使用nullify.关于常见的Fortran错误的精彩页面(带有代码)样本已删除):

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):

许多人认为从未关联的指针的状态为.not.联系.这是错误的. (...)当声明一个指针时,其状态是不确定的,并且不能使用associated内在函数安全地查询它.

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.

看起来gfortran编译器可以设置为显式地使声明中的指针无效-您可能会想到这种情况,例如编译器会自动将声明的变量设置为零,而不依靠该行为.如果您想确定的话,请自行将其取消.

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.

修改:

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

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

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

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

这篇关于派生类型中的关联指针? gFortran与Intel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆