以下代码是否符合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 ...
答案取决于与最初调用该过程的a
相关联的实际参数的值,以及首次调用之前pa
的关联状态。