问题
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
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.
回答1:
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.
来源:https://stackoverflow.com/questions/10208938/is-passing-the-same-entity-to-arguments-with-different-intent-undefined-behavior