Path: utzoo!utgpu!water!watmath!clyde!att!pacbell!ames!umd5!mimsy!chris From: chris@mimsy.UUCP (Chris Torek) Newsgroups: comp.lang.c Subject: Re: Union type conversions Message-ID: <12628@mimsy.UUCP> Date: 21 Jul 88 22:19:32 GMT References: <5754@bloom-beacon.MIT.EDU> <1180@mcgill-vision.UUCP> <3717@ece-csc.UUCP> Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 62 >>>In article <19845@watmath.waterloo.edu> atbowler@watmath.waterloo.edu >>>>(Alan T. Bowler [SDG]) wrote: >>>>... there is no guarantee that the compiler does not simply do the >>>>equivalent of `#define union struct' ... >>In article <3714@ece-csc.UUCP> jnh@ece-csc.UUCP (Joseph Nathan Hall) >>answered with a quote from K&R 1st ed., p. 140: >>> "In effect, a union is a structure in which all members have >>> OFFSET ZERO [emphasis added] ..." >In article <12490@mimsy.UUCP> chris@mimsy.UUCP (Chris Torek) I said: >>The point of this quote is to warn users that writing on any one >>element of a union *may* stomp any other element, not that it *must* >>stomp other elements. ... In article <3717@ece-csc.UUCP> jnh@ece-csc.UUCP (Joseph Nathan Hall) continues: >The description of unions in K&R (1st ed.; I don't have the 2nd close by >to look at) is, I agree, somewhat vague. But it specifically states, in >the passage I quoted above, that all of the members start at offset >zero ... don't you think this implies, without ambiguity, that the members >of a union will a) be allocated space starting at the same address, and b) >that they will have in common the first n bytes of storage, where n is the >size of the smallest item? (Notwithstanding cases where you have unions >of structs where storage isn't contiguously allocated, of course.) I am not sure. In particular, if there is no testable assertion that makes a union different from a structure, then a compiler that implements a union as a structure will not break any (testable) rules and will thus be correct. >Also, on p. 197 of the 1st ed., "A union may be thought of as a structure >all of whose members begin at offset 0 and whose size is sufficient to >contain any of its members. At most one of the members can be stored in >a union at any time." This is a bit stronger (being in the prescriptive text), but `may be thought of' is not the same as `is'. >I don't see how you can come up with the liberal interpretation that a >compiler following the K&R standard could "#define union struct." Write some correct code that produces a wrong answer if a union of a set of elements were implemented as a structure containing all the elements, and you will have a proof. As it is, the only thing I can come up with is this: union { int a; int b; } x; ... x.b = 0; x.a = 123; assert(x.b == 123); which I am not certain is guaranteed (by K&R 1st ed., at least) to work. If it did not, it would violate the Rule of Least Astonishment, but that rule does not appear in the text. . . . -- In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163) Domain: chris@mimsy.umd.edu Path: uunet!mimsy!chris