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

查看:13
本文介绍了派生类型中的关联指针?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

使用 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 上使用英特尔 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 与英特尔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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