Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!husc6!cmcl2!brl-adm!adm!STROBL%DBNGMD21.BITNET@wiscvm.wisc.EDU From: STROBL%DBNGMD21.BITNET@wiscvm.wis (Wolfgang Strobl 49+228303223) Newsgroups: comp.lang.c Subject: Re: How (not) to Program in C (was Re: PD C as solution to binary Message-ID: <9072@brl-adm.ARPA> Date: Mon, 31-Aug-87 12:43:55 EDT Article-I.D.: brl-adm.9072 Posted: Mon Aug 31 12:43:55 1987 Date-Received: Fri, 4-Sep-87 05:27:16 EDT Sender: news@brl-adm.ARPA Lines: 80 In a message with timestamp 29 AUG 87 07:39:56 Doug Gwyn writes > In article <9057@brl-adm.ARPA> STROBL%DBNGMD21.BITNET@wiscvm.wis (Wolfgang > Strobl 49+228303223) writes: > >There is nothing more to say, I think. > > Wrong -- you could explain why you quote a passage from K&R that > backs up the statement you were taking exception to. With pleasure. Please excuse errors in my language; it's difficult to do such hairsplitting as we do here in a foreign language. The paragraph from K&R I quoted in my reply had an explicit statement about what unions are good for (be refered to the fragments ".. compiler keeping track of size and alignment requirements ... without embedding any machine-dependent information in the program ..."; I don't want to repeat the quote). The statement I took exception to did not mention these virtues but banned Unions because of potential abuse. So far I do not understand what remains to be explained, but you are right: there is more to say, I take that back. The original message of Brandon Allbery said | | Unions -- are NEVER necessary. The only legal use of a union is to shrink a | variable which can contain a number of different types of values; a struct | will work just as well, at a (possibly large) space penalty. Illegal uses, | ... (rest omitted) This gives a rule: Unions are NEVER necessary and explains it with an exception to that rule: the use of Unions i s necessary if you don't have an arbitrary amount of space to your disposal (as the proposed replacement of Unions by Structs would require). This I called a contradiction. Now, if it makes no difference to you whether your program needs say ten times more space, you may replace portably used Unions by Structs. But even then it makes no sense. Consider any program which makes portable use of Unions (in the way described in K&R). It contains declarations of variables which may contain values of different types at different times, but only one type and value at any point in time. All C compilers I know of are not able to check whether a program acts that way, but with a runtime check it can be done. At least the information (that a structured variable will have only one value in one of a its members at any one time) is in the source program. Please note that I do not suggest a type tag for Unions which is accessable to the programmer. Now replace all Unions by Structs, as proposed. Please explain why this is any better. Will a program which stores one member and uses another later be less erroneous then? Very seldom. The only changes I see here are - programs in which the programmer erroneously assumed no overlapping of variable usage and used Unions now work (the store mem1, store mem2, use mem1 case). Such programs are probable wrong anyhow :-) - programs where the program erroneously accesses a variable via the wrong member because of a programming error now behave differently: Union: store mem1, use mem2 gave the machine dependent bit pattern of the mem1 value interpreted as mem2 value. This normally creates machine dependent garbage. Struct: the above sequence gives the last value stored via mem2, if any. The result may be garbage, zero or an usable, but nevertheless wrong value. - after the change one cannot say from the program text alone whether some usage patterns for a variable are right or wrong. Some static and dynamic checking possibilities are lost. Conclusion: I find Unions usefull as a construct which allows me to express usage restrictions to a structured variable in a formal way, checkable by the compiler and me, with the added bonus of big space savings. Wolfgang Strobl, GMD Z1.BN, STROBL@DBNGMD21.BITNET