structure function returns -- how?

Daniel R. Levy levy at ttrdc.UUCP
Mon Jan 12 07:35:12 AEST 1987


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.
-- 
 -------------------------------    Disclaimer:  The views contained herein are
|            dan levy            |  my own and are not at all those of my em-
|         an engihacker @        |  ployer or the administrator of any computer
| at&t computer systems division |  upon which I may hack.
|        skokie, illinois        |
 --------------------------------   Path: ..!{akgua,homxb,ihnp4,ltuxa,mvuxa,
                                        allegra,ulysses,vax135}!ttrdc!levy



More information about the Comp.lang.c mailing list