Is passing the same entity to arguments with different intent undefined behavior?

你。 提交于 2020-01-25 06:11:50

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!