return(struct) implementation

Henry Spencer henry at utzoo.uucp
Wed Mar 8 02:39:52 AEST 1989


In article <4425 at pt.cs.cmu.edu> marcoz at 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 at zoo.toronto.edu



More information about the Comp.lang.c mailing list