From: Niklas Holsti <niklas.holsti@tidorum.invalid>
Subject: Re: Passing the same actual as both in and out formal parameters?
Date: Tue, 17 Nov 2009 12:40:57 +0200
Date: 2009-11-17T12:40:57+02:00 [thread overview]
Message-ID: <4b027ddd$0$3877$4f793bc4@news.tdc.fi> (raw)
In-Reply-To: <1fbe454c-52b0-408b-9159-982fc019a53c@j19g2000yqk.googlegroups.com>
Ludovic Brenta wrote:
> Consider:
>
> type T is tagged private;
> procedure P (A : in T; B : out T) is separate;
> Object : T;
> begin
> P (A => Object, B => Object);
>
> This seems legal but I suspect the execution might lead to bugs if P
> reads and writes components of A and B in arbitrary order, e.g.
I think this situation is defined in RM 6.2(12) where A and B are
defined as "distinct access paths" to the same object. It is a bounded
error if the parameter passing mechanism is not specified, but (by
default) should work as expected when the parameters are passed by
reference.
> type T is tagged record
> L, M : Integer;
> end record;
>
> procedure P (A : in T; B : out T) is
> begin
> B.L := A.M; -- does this change A.L too?
Yes, as far as I understand RM 6.2(12).
> B.M := A.L; -- bug: A.L has been clobbered, now B.M = B.L?
I believe so.
> end P;
>
> My concern stems from the fact that T is tagged (I cannot change
> that), so Object is passed by reference as both A and B.
>
> Am I right to be concerned?
Yes, if you expect A to be immutable during the execution of P.
There is a Note to RM 6.2(12), which is 6.2(13): "A formal parameter of
mode in is a constant view (see 3.3); it cannot be updated within the
subprogram body". But I think this means only that the "in" mode access
path to this object cannot be used to update it. It does not mean that
the value of the object cannot change at all, due to assignments from
other access paths.
If P is mean to return B as A with L and M swapped, you should use an
aggregate assignment, B := (L => A.M, M => A.L).
--
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
. @ .
next prev parent reply other threads:[~2009-11-17 10:40 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-11-17 9:50 Passing the same actual as both in and out formal parameters? Ludovic Brenta
2009-11-17 10:31 ` Jean-Pierre Rosen
2009-11-17 11:26 ` Ludovic Brenta
2009-11-17 13:13 ` Jean-Pierre Rosen
2009-11-17 16:07 ` Ludovic Brenta
2009-11-18 10:00 ` Jean-Pierre Rosen
2009-11-17 10:40 ` Niklas Holsti [this message]
2009-11-17 16:26 ` Adam Beneschan
2009-11-17 21:25 ` Randy Brukardt
2009-11-18 0:11 ` Jeffrey R. Carter
2009-11-18 0:23 ` Adam Beneschan
2009-11-18 3:47 ` Jeffrey R. Carter
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox