Newsgroups: comp.lang.c
Path: utzoo!henry
From: henry@utzoo.uucp (Henry Spencer)
Subject: Re: Does ANSI prohibit assignments between overlapping union components?
Message-ID: <1990Feb18.004521.25747@utzoo.uucp>
Organization: U of Toronto Zoology
References: <21694:03:07:21@stealth.acf.nyu.edu>
Date: Sun, 18 Feb 90 00:45:21 GMT

In article <21694:03:07:21@stealth.acf.nyu.edu> brnstnd@stealth.acf.nyu.edu (Dan Bernstein) writes:
>   u.c.s = u.s; puts(u.c.s.c);
>... Is there an ANSI rule prohibiting this? If not, quite a
>few supposedly conformant compilers are broken.

The Holy Scriptures (Oct 88 draft), epistle 3.3.16.1, sayeth:

	"If the value being stored in an object is accessed from
	another object that overlaps in any way the storage of
	the first object, then the overlap shall be exact and
	the two objects shall have... a compatible type; otherwise
	the behavior is undefined."

The underlying reasoning here is that (a) assignment must, in general,
go via a temporary if overlapping operands are to work properly, (b) it
is very difficult for the compiler to tell when a temporary is required,
and (c) use of a temporary can impose a serious efficiency penalty.  So
C has historically required you to provide the temporary yourself if it
is necessary, and ANSI C continues that rule.
-- 
"The N in NFS stands for Not, |     Henry Spencer at U of Toronto Zoology
or Need, or perhaps Nightmare"| uunet!attcan!utzoo!henry henry@zoo.toronto.edu
