Re: Arguments with a target attribute



On May 21, 11:05 am, Victor <vgfi...@xxxxxxxxx> wrote:
On May 21, 12:27 am, nos...@xxxxxxxxxxxxx (Richard Maine) wrote:





Victor <vgfi...@xxxxxxxxx> wrote:
Ok... I thought that actual arguments were passed by reference on
entry to a procedure.

You thought wrong. That is a common misconception. Pass-by-reference has
never been specified by the standard. Quite to the contrary, the
standard goes to a *LOT* of trouble to make sure that this is not
specified. Several parts of the standard are a bit messy specifically in
order to allow implementations the freedom to use any of several
argument passing mechanisms. Assuming pass-by-reference will cause you
to write nonstandard and buggy code; the bugs are particularly likely to
show up when you turn on compiler optimization options. Some of the
freedom allowed compilers is there in order to allow optimizations.
There are also some situations in f90 and later where pass-by-reference
is impractical and is never done.

I don't have time to craft examples tonight. Some of the scenarios can
get quite complicated. Just start by throwing out any assumptions you
have that the standard requires pass by reference. If you have any code
that assumes pass by reference, then you probably already have an
example of something likely not to work in all cases. If you don't have
any such code, well then why are you citing such an assumption?

For a trivial example

   program main
      call sub(x,x)
   end
   subroutine sub(x,y)
      x = 2.0
      y = 42.0
      z = x + y
      write (*,*) z
   end

This code is illegal because its behavior depends on the argument
passing mechanism. That's not the way the standard phrases the
restrictions that this code violates. The standard-speak is much messier
than that. But that's the essence of it. If argument passing were really
done as pass by reference, this would print 84.0 because both x and y
would be 42.0 when z = x + y were executed. For kicks, I just tried this
with gfortran. When compiled without optimization, it prints 84.0, but
when I specify -O2, it prints 44.0, which indicates that it does not
strictly do pass by reference.

The code is illegal per the standard, so the gfortran behavior is
allowed. The behavior is perhaps even expected, at least by me; that's
why I tried it.

--
Richard Maine                    | Good judgment comes from experience;
email: last name at domain . net | experience comes from bad judgment.
domain: summertriangle           |  -- Mark Twain

Wow!!!!  This is an eye opener to me.  I read in one book which I will
not mention that Fortran passes arguments by reference.  This is so
confusing.  You have to write code not knowing what the compiler will
implement.  I'm wondering if there is a compiler flag which forces one
or the other.- Hide quoted text -

- Show quoted text -

Some more thought on this...

I come from a C++ background. In C++ everything is passed by value
unless otherwise specified through a reference or a pointer. It seems
to me that Fortran programmers are more at the mercy of which compiler
they use (or which flags they use as shown in one of the examples
above.) This makes Fortran programming more complicated. You really
need to know the compiler you're using, and if you're compiling with
several compilers, well, good luck. I may be wrong but this are my
first impressions.
.