Reputation: 12567
I saw this code on the Internet: dot_product(x, x)
Is this "undefined behavior" in Fortran (because of the aliasing)?
Upvotes: 2
Views: 684
Reputation: 12567
I finally tracked down what the FORTRAN 77 ANSI standard has to say about this:
15.9.3.6 Restrictions on Association of Entities
If a subprogram reference causes a dummy argument in the referenced subprogram to become associated with another dummy argument in the referenced subprogram, neither dummy argument may become defined during execution of that subprogram. For example, if a subroutine is headed by
SUBROUTINE XYZ (A,B)
and is referenced by
CALL XYZ (C,C)
the dummy arguments A
and B
each become associated with the same actual argument C
and therefore with each other. Neither A
nor B
may become defined during this execution
of subroutine XYZ
or by any procedures referenced by XYZ
.
So the standard restricts the modification of the aliased arguments rather than their aliasing.
Upvotes: 0
Reputation: 78316
No, there is no undefined behaviour there, the dot product of a vector with itself is well defined. The function definition does not require any modification to the input arguments, it would be a bizarre implementation indeed which modified them.
The (2003 and 2008) standard's restrictions on arguments which overlap (or are aliased but that is not the language the standard uses) only apply if a procedure tries to redefine such an argument. That is not the case with the intrinsic dot_product
whose arguments, as defined in the standard(s), have intent(in)
.
Upvotes: 4