有没有一种方法可以检查是否在Fortran子例程中发生别名,或者至少可以告诉编译器发出警告?
考虑下面的示例(比较简单):
module alias
contains
subroutine myAdd(a, b, c)
integer,intent(in) :: a, b
integer,intent(inout) :: c
c = 0
c = a + b
end subroutine
end module
program test
use alias
integer :: a, b
a = 1 ; b = 2
call myAdd(a, b, b)
print *, b, 'is not 3'
end program
在此,结果在子例程中设置为零。如果给定相同的变量作为输入和输出,则结果(显然)是错误的。有没有办法在运行时或编译时捕获这种别名?
是的,gfortran将使用编译器选项检测到一些别名-Waliasing
,但是,参数必须具有intentin
和out
。它不适用于您的示例,因为您已将参数声明c
为intent(inout)
。在此示例中,您可以仅将意图更改为,out
因为c
未使用的输入值。他们尝试编译器选项!gfortran输出:
alias.f90:17.16:
call myAdd(a, b, b)
1
Warning: Same actual argument associated with INTENT(IN) argument 'b' and INTENT(OUT) argument 'c' at (1)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句