是否将同一实体传递给具有不同意图未定义行为的参数? [英] Is passing the same entity to arguments with different intent undefined behavior?
问题描述
module foo
contains
subroutine bar()
integer :: i(3)
i(1) = 1
i(2) = 2
i(3) = 3
call baz(i, i)
end subroutine
subroutine baz(a,b)
integer, intent(in) :: a(:)
integer, intent(inout) :: b(:)
b(2) = 5
print *, a
print *, b
end subroutine
end module
program xx
use foo
call bar()
end program
在这段代码中,我将相同的数组i
传递给baz,将其绑定到具有不同意图的参数.当然,当我打印a
时,它会改变.这是未定义的行为,还是符合规范?
In this code, I am passing the same array i
to baz, binding it to arguments having different intent. Of course, when I print a
, it changes. Is this undefined behavior, or it is according to specification ?
请注意,我完全希望这会发生.我对这种行为并不感到困惑,我只想了解它是否有效.
Note that I fully expect this to happen. I am not puzzled by the behavior, I just want to understand if it's valid or not.
推荐答案
这是未定义的行为. Fortran通常禁止参数别名(即,多个参数指向相同的实际数据),除非这些参数具有POINTER或TARGET属性.
It is undefined behavior. Fortran generally prohibits argument aliasing (i.e. several arguments pointing to the same actual data), unless the arguments have the POINTER or TARGET attributes.
这篇关于是否将同一实体传递给具有不同意图未定义行为的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!