Re: how to use %val() ???
- From: Dave Thompson <david.thompson1@xxxxxxxxxxxxxxxx>
- Date: Mon, 17 Oct 2005 02:17:05 GMT
On Fri, 07 Oct 2005 19:38:42 GMT, "James Giles"
<jamesgiles@xxxxxxxxxxxxxxxx> wrote:
> 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
for parameters/formals/dummies declared as pointer, or as array (which
_for a parameter only_ in C is really a pointer)
> 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.
>
Right.
> 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.)
>
The C code will set the actual to and output 100.0 twice.
(Actually, if we consider %g as closest corresponding to
list or "free" format, it might be 100 twice. But that's
irrelevant to your and my points.)
> In spite of this being illegal, most Fortran
> implementations won't detect the error and will
> permit the call. The result *might* be <snip>
> 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
Although depending on the processor (caching) and perhaps OS this
might not actually be any slower. And in this example the difference
will be swamped by the cost of doing even one I/O, but of course in
real computational code that probably wouldn't be there.
> 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
C99 also added complex. Odd coincidences, those. :-)
> 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).
>
Although F9x POINTERs have in some cases more specific target types
than C pointers do, and thus may produce somewhat less false aliasing.
> 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.
Strongly agree.
- David.Thompson1 at worldnet.att.net
.
- 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
- Re: how to use %val() ???
- From: James Giles
- g77 : how to use %val() ???
- Prev by Date: Re: Enumerations in fortran 2003
- 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
|