Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: utzoo!mnetor!seismo!rochester!pt.cs.cmu.edu!sei.cmu.edu!firth
From: firth@sei.cmu.edu (Robert Firth)
Newsgroups: comp.lang.c
Subject: Re: structure function returns -- how?
Message-ID: <519@aw.sei.cmu.edu.sei.cmu.edu>
Date: Mon, 12-Jan-87 10:00:33 EST
Article-I.D.: aw.519
Posted: Mon Jan 12 10:00:33 1987
Date-Received: Mon, 12-Jan-87 21:57:41 EST
References: <131@hcx1.UUCP> <773@maynard.BSW.COM>
Sender: netnews@sei.cmu.edu
Reply-To: firth@bd.sei.cmu.edu.UUCP (PUT YOUR NAME HERE)
Organization: Carnegie-Mellon University, SEI, Pgh, Pa
Lines: 37

In article <1438@ttrdc.UUCP> levy@ttrdc.UUCP (Daniel R. Levy) writes:
>In article <505@aw.sei.cmu.edu.sei.cmu.edu>, firth@sei.cmu.edu.UUCP writes:
>>In article <326@bms-at.UUCP> stuart@bms-at.UUCP (Stuart D. Gathman) writes:
>>>*** The real solution ***
>>>(f)	For an assignment, the caller passes a pointer to the object
>>>	being assigned.  The function does the copy.
>>>Stuart D. Gathman	<..!seismo!dgis!bms-at!stuart>
>>Sorry.  This fails if the called function accesses the destination
>>object via another path.  Consider for instance
>>Z1 : COMPLEX;
>>Z1 := SWAP_RE_AND_IM(Z1);
>>with parameter and result object implemented by reference. [*]
>>The code of the SWAP has
>>	return COMPLEX'(ARG.IM,ARG.RE);
>>If the result is assigned piecemeal to the result place,
>>then Z1.RE will have been overwritten.  This is clearly a
>>compiler bug, since nearly all languages specify that the
>>RHS shall be completely evaluated BEFORE the assignment is
>>performed, and the error arises because the implementation
>>violates those semantics.
>
>This "failure" can only happen if the arguments are passed by reference,
>as well as the destination.  If they are passed by value, or if the ap-
>parent passing of struct arguments by value is really implemented by passing
>them by reference but the called routine (with struct-valued return value)
>is "smart" enough to use copies in cases of overlap instead of the originals,
>you avoid this problem.

And I said that[*].  I also said 'for instance', in the naive belief that
one example would be enough of a hint to show why the general method is
wrong.  So here is another 'for instance': the called function accesses
the result object directly, and not via a by-reference parameter.
Same erroneous behaviour, and the only fix is NOT to pass a pointer to
the result object.  Nor is this aliasing; the function has no logical
visibility of its result object.

Apologies to the rest of you for beating this thing to death.