structure function returns -- how?

Henry Spencer henry at utzoo.UUCP
Sat Dec 13 08:09:06 AEST 1986


>Suppose   a   is declared as a structure and   b   is a function which 
>returns a structure.  In the statement:
>                       a = b () ;
>when and how should the copying into   a   take place?

It's an awkward problem, since struct values generally don't fit in the
registers that are used to return ordinary values.  The best solution is
for the caller to allocate space for the returned value and communicate
the address to the callee somehow, so the callee can copy the value there
before returning.  This does require that the caller know the returned
type, and there is a lot of sloppiness about this in C, especially when
the returned value is not being used.  (Although said sloppiness may be
less common for struct-valued functions.)  There can also sometimes be
difficulties in implementing it.  There are various alternatives, some
of which indeed are not signal-proof.  Using a static area to return the
value leads to trouble in the (quite uncommon) case of struct-returning
functions being used in signal handlers, but has the virtue of being easy
to retrofit into old compilers.
-- 
				Henry Spencer @ U of Toronto Zoology
				{allegra,ihnp4,decvax,pyramid}!utzoo!henry



More information about the Comp.lang.c mailing list