comp.lang.ada
 help / color / mirror / Atom feed
* Re: Call by reference vs. call by value
@ 1996-07-25  0:00 Marin David Condic, 407.796.8997, M/S 731-93
  1996-07-26  0:00 ` Peter Amey
  0 siblings, 1 reply; 29+ messages in thread
From: Marin David Condic, 407.796.8997, M/S 731-93 @ 1996-07-25  0:00 UTC (permalink / raw)



Peter Amey <pna@ERLANG.PRAXIS.CO.UK> writes:
>This is another Ada feature well covered by the SPARK subset.  The rules
>of SPARK (which are checked by the SPARK Examiner) prohibit all cases
>of aliasing where program meaning might be affected by the parameter
>passing mechanism used. A SPARK program has copy-in, copy-out semantics
>regardless of the compiler used to compile it.
>
    While I understand the interest in making language mechanisms
    secure and predictable, I sometimes wonder about things like this
    reference/value debate. It seems like a solution desparately
    seeking a problem.

    My experience with software bugs (especially in Ada) is that the
    overwhelming bulk of them are some version of the computer doing
    what you told it to do, not what you meant for it to do. (The
    implementer got the logic wrong) Most forms of semanitc induced
    errors are the kinds of things caught by the compiler (assigning
    reals to integers without explicit conversion, uninitialized
    variables, etc.) Those few weird "corner-case" bugs that fall into
    the category of "the LRM said it was 'implementation dependent'
    and my implementation bit me on the butt" happen so infrequently
    (although are annoyingly difficult to find!) as to not be worth
    the speed penalties imposed by "absolutely provable secure"
    implementations.

    I'd favor passing parameters by reference (for speed) over copy
    mechanisms because in the practical world of "the way programmers
    *really* program", my experience is thus: 1) You almost never see
    code (in the real world) where parameter passing mechanisms cause
    the problems (id est, where compiler "A" would have got it right
    and compiler "B" got it wrong) 2) Most (Ada) compilers are pretty
    good at finding the bulk of the parameter-passing problems no
    matter what the implementation mechanism is through forms of
    static analysis. (They're also pretty smart about figuring out
    what's the *optimal* way to pass the parameters, depending on data
    types.)

    I think we've learned by now that it doesn't help a language to be
    syntactically/semantically "perfect" (curing all the "Language
    Lawyer" problems) at the expense of being slow, cumbersome and
    late to market.

    MDC

Marin David Condic, Senior Computer Engineer    ATT:        407.796.8997
M/S 731-96                                      Technet:    796.8997
Pratt & Whitney, GESP                           Fax:        407.796.4669
P.O. Box 109600                                 Internet:   CONDICMA@PWFL.COM
West Palm Beach, FL 33410-9600                  Internet:   CONDIC@FLINET.COM
===============================================================================
    "Government is not reason. It is not eloquence. It is a force.
    Like fire, a dangerous servant and a fearful master."

        --  George Washington
===============================================================================




^ permalink raw reply	[flat|nested] 29+ messages in thread
* Call by reference vs. call by value
@ 1996-07-20  0:00 Christopher Felaco
  1996-07-20  0:00 ` James A. Krzyzanowski
                   ` (2 more replies)
  0 siblings, 3 replies; 29+ messages in thread
From: Christopher Felaco @ 1996-07-20  0:00 UTC (permalink / raw)



I came across a problem with a procedure that resembled the following:

type Simple_Vector is array (1 .. 2) of Float;

procedure Vector_Manipulation (X : in Simple_Vector;
                               Y : out Simple_Vector) is
begin

    Y (1) := X (1) + X (2);
    Y (2) := X (1) * X (2);

end Vector_Manipulation;

I was told that calling this procedure with the same in and out
parameters as follows would give unexpected results:

    Vector_Manipulation (X => Test_Vector, Y => Text_Vector);

The reason is that both parameters may be passed by reference, hence
modifying Y in the first line, modifies X as well, and the value of X
used in the second line is not what was desired.  I was surprised by
this, it sounds like a typical problem in C or C++, not something likely
to happen in Ada.  So I did some research and concluded from section 6.2
of the Ada 95 RM that it is entirely up to the compiler to determine how
arrays of simple elementary values are called.  I recall also reading
that ACT got itself in a bit of trouble by changing the behavior of gnat
to use call by reference for all records.

My reasons for posting this are twofold.  The first reason is to verify
that this is correct, and see if anyone else has been bitten by this
particular undefined behavior.  The second reason is to express some
disappointment.  I think in this area, C++ has a big advantage in that
it forces the programmer to be aware of whether a function uses
call-by-refernce of call-by-value semantics.  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.

I realize that the solution to this problem in this case is to simply
make one assignment using an array aggregate, or change the procedure to
a function.  I have recommended this course of action.  I still think
that this type of error should not occur.  In many situations, the
solution may not so simple.  Ada's behavior should not be so
"implementation dependent".

- Chris




^ permalink raw reply	[flat|nested] 29+ messages in thread

end of thread, other threads:[~1996-08-05  0:00 UTC | newest]

Thread overview: 29+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-07-25  0:00 Call by reference vs. call by value Marin David Condic, 407.796.8997, M/S 731-93
1996-07-26  0:00 ` Peter Amey
  -- strict thread matches above, loose matches on Subject: below --
1996-07-20  0:00 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 A Duff
1996-07-27  0:00             ` Peter Morris
1996-07-28  0:00               ` Robert A Duff
1996-07-23  0:00           ` Robert Dewar
1996-07-24  0:00             ` Robert A Duff
1996-07-24  0:00           ` Richard A. O'Keefe
1996-07-22  0:00   ` Felaco
1996-07-22  0:00     ` Robert Dewar
1996-07-22  0:00     ` Robert A Duff
1996-07-30  0:00       ` Richard A. O'Keefe
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

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