unions as function arguments

Mikel Lechner mikel at teraida.UUCP
Tue Mar 21 20:04:01 AEST 1989


In article <892 at fornax.UUCP> stevec at lccr.cs.sfu.ca (Steve Cumming) writes:
> What I want to know is, is it legal to pass a union to a function?

Yes, just like it is legal to pass a structure to a function.

> In that case passing a constant 0 as a union should cause the compiler to
> do the obvious thing.  It don't. 

No.  Passing an integer argument and passing a union containing an integer
are not the same thing.  This works on most systems because the union and
integer are aligned the same, passed the same way, and are the same size.
However, this is not strictly correct C.  I ran into exactly this same
problem with some code I inherited, when first porting to the Sun4.

Sun took the liberty with the Sun4 to pass structs and unions differently
from scalar arguments.  The register window architecture of the SPARC is
used to pass scalar arguments through registers.  Non-scalar arguments are
passed on the stack the old-fashioned way.  If you pass an int to a
function expecting a union.  The caller puts the argument on the stack,
and the receiving fuction looks for it on the stack.  And the program
usually gets a segmentation violation.

Mikel Lechner			decwrl!teraida!mikel
Teradyne EDA			(408) 980-5200
5155 Old Ironsides Drive
Santa Clara, Ca 95054

[[ Isn't that what I said?  --wnl ]]



More information about the Comp.sys.sun mailing list