comp.lang.ada
 help / color / mirror / Atom feed
From: Felaco <bcf@swlpts1.ssd.ray.com>
Subject: Re: Call by reference vs. call by value
Date: 1996/07/22
Date: 1996-07-22T00:00:00+00:00	[thread overview]
Message-ID: <wvyspakfnn4.fsf@swlpts1.ssd.ray.com> (raw)
In-Reply-To: dewar.837901109@schonberg


dewar@cs.nyu.edu (Robert Dewar) writes:

> 
> Chris said
> 
>    This is somewhat of a pain
>    at times, but at least it is obvious and clearly defined.  I had no idea
>    that Ada's behavior was undefined and wrongly assumed that in parameters
>    were always passed by value.
> 
> Well you can always get bitten by not knowing a language rule that is
> clear, and you can always be surprised if you wrongly assume something!
> Ada's behavior is not undefined, it is implementation defined (there is
> an important difference!)
> 
> It is quite odd to assume that all IN parameters are passed by value, do
> you really expect million element arrays to be copied when they are passed
> to procedures, I think not.

Of course I have to admit ignorance here.  I never really thought
about it in Ada.  For some reason I assumed that the compiler would in
some way ensure that this type of error would not occur, either by
passing the array by value, or reordering the assignments so the
problem would not occur.  I realize I expected too much.

> Here is a case where the language semantics is non-deterministic for
> efficiency reasons. To force call by value in all cases would be clearly
> unaccepable for large arrays. On the other hand, to force call by
> reference is also inefficient on some machines. Yes, the semantics would
> be cleaner if there were no implementation dependence here, but efficiency
> issues certainly cannot be ignored.

Well, I guess I never conceived a machine might exist where call-by-
value for anything bigger than a word is more efficient than call by
reference, so my first instinct would be to dictate that
call-by-reference be used in all cases.  I believed the opposite was
true, however, because I wrongly thought that the designers of Ada
valued safety over performance, which is not really true.

> The question is, does someone who knows Ada get into trouble with
> this rule?  Clearly if you don't know a rule in the language you can
> always get into trouble, you can't expect Ada to automatically
> correct your misconceptions about the language. This is certainly a
> lesson that you need to know a language well to avoid trouble, but
> it is not convincing that there is a problem here.

Of course one should know a language well, but in this case the
language "wasn't very friendly".  My point about C/C++ is that the
programmer is forced to think about this issue immediately.  All
arrays are passed by reference, and all structs are passed by value -
no ambiguity about it.  You have to be aware of this to accomplish
anything in the language.  I learned of this problem within a month of
programming in C, but I've been using Ada for a year, and this is the
first time I've had this issue pop up.  Maybe this reflects poorly on
me, but what can I say!
 
> Actually, I think most people who make a mistake here make a mistake the
> other way round, they assume that arrays (and even records) have to be
> passed by reference.

Sure, don't doubt it.  It makes perfect sense.

> You actually remember the GNAT situation wrong, and if you remembered it
> right, it would have reminded you of the issue. We changed record passing
> from being by reference for large records to being always by value, as you
> would like to see. This turned out to be completely unacceptable in some
> situations because of severe degradation of performance -- so this is a
> nice object lesson that your recommendation is infeasible!

For the record, my recommendation is simply to clarify the issue.
It would be nice if there were no implementation defined behavior, in
a perfect computer world, but I'm not that naive.

One possibility that hasn't been explored is this: define the rule as
call-by-value *by default*, and give me a pragma or something to get
the more efficient call-by-reference behavior.  Then I would be forced
to be aware of the pitfalls.  It's not the greatest solution, but it
least it gives the programmer more control.
  
> P.P.S. In C++ you can't pass arrays anyway so the issue does not arise, so it
> is wrong to say that the mechanism for this is defined in C++! If you cold
> pass arrays in C or C++, then the same problem would arise.

That's exactly my point, it is defined by not being possible!

-- 
-------------------------------------------------------------------------------
Chris Felaco                               Phone: x4631 (Raynet 444, Local 842)
Raytheon Company                                         Email: bcf@ssd.ray.com
-------------------------------------------------------------------------------




  parent reply	other threads:[~1996-07-22  0:00 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-07-20  0:00 Call by reference vs. call by value Christopher Felaco
1996-07-20  0:00 ` James A. Krzyzanowski
1996-07-20  0:00   ` Robert Dewar
1996-07-20  0:00 ` Robert Dewar
1996-07-21  0:00   ` Robert A Duff
1996-07-21  0:00     ` Robert Dewar
1996-07-22  0:00       ` Robert A Duff
1996-07-23  0:00         ` Peter Amey
1996-07-23  0:00           ` Robert Dewar
1996-07-24  0:00             ` Robert A Duff
1996-07-23  0:00           ` Robert A Duff
1996-07-27  0:00             ` Peter Morris
1996-07-28  0:00               ` Robert A Duff
1996-07-24  0:00           ` Richard A. O'Keefe
1996-07-22  0:00   ` Felaco [this message]
1996-07-22  0:00     ` Robert A Duff
1996-07-30  0:00       ` Richard A. O'Keefe
1996-07-22  0:00     ` Robert Dewar
1996-07-22  0:00   ` Karl Cooper {46901}
1996-07-22  0:00     ` Robert Dewar
1996-07-30  0:00   ` Felaco
1996-07-31  0:00     ` Robert A Duff
1996-08-02  0:00     ` Robert Dewar
1996-08-03  0:00     ` JP Thornley
1996-08-05  0:00       ` Roderick Chapman
1996-07-21  0:00 ` Robert A Duff
1996-07-21  0:00   ` Robert Dewar
  -- strict thread matches above, loose matches on Subject: below --
1996-07-25  0:00 Marin David Condic, 407.796.8997, M/S 731-93
1996-07-26  0:00 ` Peter Amey
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox