Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!henry From: henry@utzoo.UUCP (Henry Spencer) Newsgroups: comp.lang.c Subject: Re: structure function returns -- how? Message-ID: <7403@utzoo.UUCP> Date: Fri, 12-Dec-86 17:09:06 EST Article-I.D.: utzoo.7403 Posted: Fri Dec 12 17:09:06 1986 Date-Received: Fri, 12-Dec-86 17:09:06 EST References: <131@hcx1.UUCP>, <773@maynard.BSW.COM> Organization: U of Toronto Zoology Lines: 21 >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