意图(out)和意图(inout)之间的区别 [英] Difference between intent(out) and intent(inout)

查看:444
本文介绍了意图(out)和意图(inout)之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据Fortran标准:


非指针伪参数的INTENT(OUT)属性指定了伪参数变为未定义调用过程

然而,这个简单的代码给了我 5 作为输出,所以看起来这个参数在程序开始时并没有变得没有定义(在本例中是一个子程序)。

 子程序无用(a)
整数,意图(out):: a
print *,a
结束子程序无用

程序测试
整数:: n = 5
调用无用(n)
结束程序测试

错误?看来 intent(inout) intent(out)是相同的。

解决方案

intent(inout) intent(out)肯定不一样。你已经注意到了,尽管你没有得出正确的结论。进入子程序无用 a 是未定义的,而不是定义的。

变量未定义意味着您在引用它时不能依赖于特定的行为。您发现变量 a 的值 5 ,但这并不意味着您可以观察到的唯一值是 5 。特别是未定义并不意味着像NaN一样具有特定的值。

由于对未定义变量的引用,您的代码不符合标准。请参阅Fortran 2008 6.2(类似的含义将在最初标记的Fortran 90中的某处)。特别值得注意的是,编译器不必指出你的错误。



使用 intent(inout)变量 a 在被引用时被定义,并且它将被保证具有值 5 (对于符合的处理器) 。

更广泛地说,两个意图属性之间还有其他区别,这个变量 a的定义的相似性的巧合外观



具有延迟类型参数的可分配数组和对象例如解除分配;派生类型变成未定义的(并且任何可分配的组件被解除分配)以及具有默认初始化的组件被重新初始化;指针的关联状态变得未定义。



所有这些后面的事情都有可能产生非常尴尬的结果,比标量整数更可能,如果它们没有被引用首先定义。

According to the Fortran standard:

The INTENT (OUT) attribute for a nonpointer dummy argument specifies that the dummy argument becomes undefined on invocation of the procedure

However this simple code gives me 5 as output, so it seems the argument didn't become undefined at the start of the procedure (in this case a subroutine).

subroutine useless(a)
  integer, intent(out) :: a
  print *,a
end subroutine useless

program test
  integer :: n=5
  call useless(n)
end program test

What am I getting wrong? It seems that intent(inout) and intent(out) are the same.

解决方案

intent(inout) and intent(out) are certainly not the same. You have noted why, although you don't draw the correct conclusion. On entering the subroutine useless a is undefined, rather than defined.

Having a variable "undefined" means that you cannot rely on a specific behaviour when you reference it. You observed that the variable a had a value 5 but this does not mean that the only value you could observe is 5. In particular "undefined" does not mean "takes a specific value like NaN".

Your code is not standard conforming because of this reference to an undefined variable. See Fortran 2008 6.2 (similar meaning will be somewhere in Fortran 90 as initially tagged). Of particular note is that the compiler doesn't have to point out your mistake.

With intent(inout) the variable a would be defined when referenced and it will be guaranteed to have the value 5 (for a conforming processor).

More widely, there are other differences between the two intent attributes and this "coincidental" appearance of the similarity of the definition of the variable a could be more troublesome.

Allocatable arrays and objects with deferred type parameters, for example, are deallocated; derived types become undefined (and any allocatable components deallocated) and components with default initialization are "re-initalized"; pointers have their association status become undefined.

All of these latter things have potential for very awkward results, much more so than with a scalar integer, if they are referenced without being defined first.

这篇关于意图(out)和意图(inout)之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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