Does ANSI prohibit assignments between overlapping union components?

Henry Spencer henry at utzoo.uucp
Sun Feb 18 11:45:21 AEST 1990


In article <21694:03:07:21 at stealth.acf.nyu.edu> brnstnd at 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 at zoo.toronto.edu



More information about the Comp.lang.c mailing list