structure function returns -- how?

Robert Firth firth at sei.cmu.edu
Tue Jan 13 01:00:33 AEST 1987


In article <1438 at ttrdc.UUCP> levy at ttrdc.UUCP (Daniel R. Levy) writes:
>In article <505 at aw.sei.cmu.edu.sei.cmu.edu>, firth at sei.cmu.edu.UUCP writes:
>>In article <326 at bms-at.UUCP> stuart at 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.



More information about the Comp.lang.c mailing list