有效程序,显示Fortran中intent(out)和intent(inout)之间的差异 [英] Valid programs showing difference between intent(out) and intent(inout) in Fortran

查看:318
本文介绍了有效程序,显示Fortran中intent(out)和intent(inout)之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在SO上找到的帖子的后续内容: intent(out)与intent(inout)之间的差异

This is a follow up to a post that I found on SO: Difference between intent(out) and intent(inout)

链接的问题通过询问

The linked question asked about the difference between intent(out) and intent(inout) in Fortran by asking an invalid program.

有人能提出一个简单有效的程序,通过将intent(inout)更改为intent(out)来产生不同的结果吗?

Could anyone come up with a simple valid program(s) that give different results by changing intent(inout) to intent(out) or vice versa?

推荐答案

与史蒂夫·莱昂内尔(Steve Lionel)的答案相同,我最初的答案也许引起了人们的兴趣,intent(out)对虚拟参数(和实际参数)初始状态的影响是回答此问题的一种方式.

As Steve Lionel's answer, and the comments in my original answer which perhaps sparked this interest, the effects of intent(out) on the initial state of the dummy argument (and the actual argument) are a way in to answering this question.

intent(inout)具有哑元参数,该哑元参数反映了程序输入时实际参数的值. intent(out)重置"伪参数.对于链接的问题,此未定义"是程序无效性质的原因.

intent(inout) has the dummy argument reflect the value of the actual argument on entry to the procedure. intent(out) "resets" the dummy argument. In the case of the linked question, this "undefinition" is the cause of the invalid nature of the program.

更准确地说,关于intent(out)伪参数,我们可以说以下几点:

More precisely, we can say the following things about intent(out) dummy arguments:

  • 可分配的实际参数被释放;
  • 指针实际参数的指针关联变为未定义;
  • 对于非指针虚拟参数,任何未默认初始化的组件都将变为未定义.

链接的问题试图引用这样一个新的未定义的值,从而犯了第三点.但是,默认的初始化组件并不是未定义的,这使我们进入了第一类有效程序:

The linked question fell foul of the third point by trying to reference such an newly undefined value. However, default initialized components are not undefined, leading us to our first class of valid programs:

  implicit none
  type t
     integer :: x=1
  end type t
  type(t) :: x=t(0)

  call s1(x)
  call s2(x)

contains
  subroutine s1(x)
    type(t), intent(inout) :: x
    print*, x%x
  end subroutine s1

  subroutine s2(x)
    type(t), intent(out) :: x
    print*, x%x
  end subroutine s2

end

重要的是,该组件的默认初始化意味着即使输入s2也不是未定义x%x,但是在过程输入之前,它与实际参数的组件可能具有不同的值.

Crucially, the default initialization of the component means that x%x isn't undefined even on entry to s2, but it takes a potentially different value from the actual argument's component prior to procedure entry.

想出一个合适的带有指针参数的程序是棘手的:具有未定义指针关联状态的指针在重新定义其指针关联状态之前不能被引用/查询.

Coming up with a suitable program with pointer arguments is tricky: a pointer with undefined pointer association status can't be referenced/queried before its pointer association status is redefined.

这让我们着眼于可分配的组件.与指针不同,在指针未定义状态下我们无法查询指针关联状态时,我们可以询问分配状态.对应于intent(out)哑元的实际参数被释放;对于intent(inout),分配状态不变:

Which leaves us looking at allocatable components. Unlike with the pointers, where we can't query pointer association status when that's undefined, we can ask about the allocation status. An actual argument corresponding to an intent(out) dummy is deallocated; with intent(inout) the allocation status is unchanged:

  implicit none
  integer, allocatable :: i
  allocate (i)

  call s1(i); print*, allocated(i)
  call s2(i); print*, allocated(i)

contains

  subroutine s1(i)
    integer, allocatable, intent(inout) :: i
  end subroutine s1

  subroutine s2(i)
    integer, allocatable, intent(out) :: i
  end subroutine s2
end

(这是Steve Lionel的示例的简单版本.)

(This is a simpler version of Steve Lionel's example.)

这一切都表明可能存在差异.错误使用intent可能会导致程序无效或含义上的重大变化.理解什么intent(in)intent(inout)intent(out)并且没有指定的意图含义是成为Fortran程序员的关键部分.

This all shows that it's possible to have differences. Using intent incorrectly can lead to invalid programs or to significant changes in meaning. Understanding what intent(in), intent(inout), intent(out) and no specified intent mean is a crucial part of being a Fortran programmer.

这篇关于有效程序,显示Fortran中intent(out)和intent(inout)之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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