Fortran 90 intent(in for an recursive function)

问题描述 投票:3回答:1

以下代码是否符合Fortran 90标准?

integer, pointer :: pa ! global
...

recursive subroutine foo(a)
integer, target, intent(in) :: a

  if (a > 10) then
    return
  endif

  if (associated(pa)) then
    pa = 123
  endif
  pa => a

  call foo(a + 1)

  ! use a

end subroutine foo

变量a声明为intent(in),根据Fortran 90 standard第5.1.2.3节:

指定在执行过程期间不得重新定义哑元参数或使其变得未定义

变量a不会在foo(a)的递归级别上重新定义;相反,我们保存了指向a的指针,因此可以在较低级别的递归上重新定义a

换句话说:

foo(a) ! do not change a, save the pointer to a
  foo(a + 1) ! change a, save the pointer to a + 1
    foo(a + 1 + 1) ! change a + 1, save the pointer to a + 1 + 1, and so on.

[基于我对标准的理解,foo(a + 1)的生存期是foo(a)的生存期的子集,因此a不应更改。对于编译器来说,假设foo()具有“未定义的行为”(或与Fortran等效的行为)是否安全?

以下代码是否符合Fortran 90标准?整数,指针:: pa!全局...递归子例程foo(a)整数,target,intent(in):: a if(a> 10)然后返回endif ...

fortran fortran90
1个回答
1
投票

答案取决于与最初调用该过程的a相关联的实际参数的值,以及首次调用之前pa的关联状态。

© www.soinside.com 2019 - 2024. All rights reserved.