Re: how to use %val() ???
- From: "James Giles" <jamesgiles@xxxxxxxxxxxxxxxx>
- Date: Fri, 07 Oct 2005 19:38:42 GMT
Richard Maine wrote:
....
> It is an oft repeated falacy that Fortran arguments are always
> passed by reference. It is, however, a falacy, at least with the
> word "always" instead of something like "often".
In fact, whereas C implementations are *required* to
conform to pass by reference semantics, few Fortran
implementations even bother to try to conform to such
limitations. Even if the implementation is ostensibly
"call by reference", local copies are often made within
the procedure.
Consider the following illegal Fortran code fragment:
A=1.0
Call sub1(A,A)
....
Subroutine sub1(X,Y)
Real :: X, Y
X = 5.0
Y = X+Y
X = X*Y
Print *, X, Y
End subroutine sub1
This is illegal since Fortran prohibits actual arguments
from being the same, overlapping, or otherwise aliased
if the corresponding dummy arguments are defined within
the procedure or anything the procedure calls in turn.
But, the corresponding C code is *not* prohibited. It
has a well defined meaning in C: the program will
output 100.0 and 10.0. On return, the C actual
argument will have value 100.0. (Note I'm assuming
that the corresponding C was written with pointer
arguments - if not, it's hardly a corresponding code.)
In spite of this being illegal, most Fortran
implementations won't detect the error and will
permit the call. The result *might* be 100.0 and
10.0, but the result also might often be 50.0 and 10.0,
or 36.0 and 6.0, or 30.0 and 6.0, or 25.0 and 5.0, etc.
The result 30.0 and 6.0 is consistent with call by
value/result semantics (if the compiler keeps both
X and Y in registers for the duration of the call, this
is that you get). The Fortran standard doesn't
*require* any of these (the call is illegal). The
code sequences that lead to these are all legal if
the arguments are not aliased. The usual rule
of thumb is that if you can tell the difference
between call by reference and call by value/result
in your code, your code is not standard.
On return, the value of A can be any of the numbers
seen above: it just depends on what order the copies
of X anY are stored into the address of the actual
arguments.
All these alternate values are generated faster
than the corresponding C90 code could manage
since the C versions must store immediately after
each assignment and reload the arguments prior to
the next assignment. With the C99 standard, programmers
can use the 'restrict' attribute and get more efficient
code - but the rules for 'restrict' are very similar
to Fortran's rules for argument association. Fortran
can get C's conventional semantic restrictions by
passing POINTERs everywhere. The targets two
POINTERs (of the same type) must be treated as
possibly aliased in Fortran (F90 or more recent).
All this is not just pedantry. If you notice that
your Fortran implementation appears to always
"pass by reference", you must still not rely on
call by reference semantics. If you do, you are
likely to end up with the worst kind of program
error: the undiagnosed production of plausible
wrong answers.
--
J. Giles
"I conclude that there are two ways of constructing a software
design: One way is to make it so simple that there are obviously
no deficiencies and the other way is to make it so complicated
that there are no obvious deficiencies." -- C. A. R. Hoare
.
- Follow-Ups:
- Re: how to use %val() ???
- From: Dave Thompson
- Re: how to use %val() ???
- References:
- g77 : how to use %val() ???
- From: Nicolas Limare
- Re: how to use %val() ???
- From: apm
- Re: how to use %val() ???
- From: glen herrmannsfeldt
- Re: how to use %val() ???
- From: apm
- Re: how to use %val() ???
- From: Dan Nagle
- Re: how to use %val() ???
- From: apm
- Re: how to use %val() ???
- From: Richard Maine
- g77 : how to use %val() ???
- Prev by Date: Re: how to use %val() ???
- Next by Date: Re: decimal to binary
- Previous by thread: Re: how to use %val() ???
- Next by thread: Re: how to use %val() ???
- Index(es):
Relevant Pages
|