Referencing overlapping arrays through pointers -
i need perform memmove()-style operation pointers tuned overlapping arrays. more precisely, need copy b(:) a(:).

in c programming language have called memmove(a, b, n). but, far know, fortran standard strict when comes pointer aliasing.
so, of following 3 options safe use (according standard), , result in undefined behaviour:
- vector syntax aliased pointers (option 1),
- explicit do-loop aliased pointers (option 2),
- call routine takes overlapping arrays arguments (option 3)
?
program ptr implicit none integer, parameter :: array_size = 10, n = 6 integer, target :: array(array_size) integer, dimension(:), pointer :: a, b integer :: => array(1: n) b => array(3: n+2) ! option 1 a(1: n) = b(1: n) ! option 2 = 1, n a(i) = b(i) end ! option 3 call foobar(a, b, n) contains subroutine foobar(a, b, length) integer, dimension(:), intent(out) :: integer, dimension(:), intent(in) :: b integer, intent(in) :: length a(1: length) = b(1: length) end subroutine foobar end program ptr
options 1 , 2 ok, compiler knows pointers can alias.
the subroutine ok per se, cannot pass there 2 aliased arguments, indeed against standard (undefined behaviour c term). can make arguments pointer, aliasing possible , program standard conforming.
integer, dimension(:), pointer :: integer, dimension(:), pointer :: b i deleted intents, because relate pointer association status, not target value, ianh commented. also, fortran2003 required them.
Comments
Post a Comment