Newsgroups: comp.lang.c Path: utzoo!henry From: henry@utzoo.uucp (Henry Spencer) Subject: Re: return(struct) implementation Message-ID: <1989Mar7.163952.20063@utzoo.uucp> Organization: U of Toronto Zoology References: <4425@pt.cs.cmu.edu> Date: Tue, 7 Mar 89 16:39:52 GMT In article <4425@pt.cs.cmu.edu> marcoz@MARCOZ.BOLTZ.CS.CMU.EDU (Marco Zagha) writes: >Can anybody explain how returning a structure from a function is >typically implemented. Assume it is too big to use registers. >Is it returned on the stack? Where does the return structure >go in relation to parameters to the function? For a stack that >grows downward, is the return value above the parameters, >below the parameters, or does the return statement overwrite >the parameters? The general answer, as you suspected, is "it depends". An additional constraint, tricky to satisfy, is that preferably no dire harm should come about if the function has not been declared properly at the site of the call (i.e. function returns struct but caller is expecting int). C programmers have a nasty tendency to assume that if they're just throwing away the return value, it doesn't matter what type it is. Just about every permutation of putting it on the stack is probably in use in some compiler or other. Probably the best approach, although it's one that requires correct declaration, is to have the caller pass in a pointer (as an invisible extra parameter) to where he wants the return value put. Even stranger approaches have been used in some implementations; the original pdp11 Ritchie compiler allocated a static struct variable for each struct-returning function, and used that as a temporary in the return sequence (yes, this meant that such functions were not completely reentrant). -- The Earth is our mother; | Henry Spencer at U of Toronto Zoology our nine months are up. | uunet!attcan!utzoo!henry henry@zoo.toronto.edu