return(struct) implementation

dave brown dave at motto.UUCP
Wed Mar 8 01:22:58 AEST 1989


In article <4425 at pt.cs.cmu.edu> marcoz at MARCOZ.BOLTZ.CS.CMU.EDU.UUCP writes:
>Can anybody explain how returning a structure from a function is
>typically implemented.

Here are two ways that I've seen.

The first is in the C compiler for Sys V/68 (Motorola's System V port for
the 68000).  Any routine that returned a struct has an invisible static
area the same size as the struct.  The return statement copies the
struct value into this area.  Code in the calling routine just
after the call, copies it to the variable which is to receive it.

The second is in the Intermetrics 68000 C compiler.  This is a cross-
compiler for the 68000, hosted under VAX Ultrix.  If a function
returns a struct, the compiler inserts a pointer on the stack in
front of the first argument.  This pointer is the address of the
receiving struct variable.  The function itself copies the return value
through the pointer into the receiving variable.

We found out about the second method when a programmer who wasn't using
the return value called the function without declaring its return type.
His first argument was then taken as the return value address, etc., etc.

 -----------------------------------------------------------------------------
|  David C. Brown		|  uunet!mnetor!motto!dave		      |
|  Motorola Canada, Ltd.	|  416-499-1441 ext 3708		      |
|  Communications Division	|  Disclaimer: Motorola is a very big company |
 -----------------------------------------------------------------------------



More information about the Comp.lang.c mailing list