Path: utzoo!mnetor!uunet!husc6!bbn!uwmcsd1!ig!jade!ucbvax!cbosgd!ihnp4!ihlpe!woods From: woods@ihlpe.ATT.COM (Swan) Newsgroups: comp.lang.c Subject: Re: A bad design decision early on in ANSI C. Message-ID: <2456@ihlpe.ATT.COM> Date: 12 Jan 88 20:25:15 GMT References: <1322@sugar.UUCP> <1942@ho95e.ATT.COM> <1373@sugar.UUCP> <4604@ihlpg.ATT.COM> Reply-To: woods@ihlpe.UUCP (52462-Swan,W.D.) Organization: AT&T Bell Laboratories - Naperville, Illinois Lines: 61 In article <4604@ihlpg.ATT.COM> tainter@ihlpg.ATT.COM (Tainter) writes: > >I think if you are going to have structure assingment you should have >structure comparison for equality (and non equality). This IN NO WAY >requires the definition of order (i.e. <, > comparisons). Anyone with >mathematic training beyond basic algebra should have no problem with this. >Why should all structures form fully ordered sets? >--j.a.tainter The problem is that the compiler cannot be sure how many bytes in the structures to compare for [in]equality. The programmer may be in for a big surprise if you naively assume that the compiler should compare every byte. Take the following code segment: struct wierd { int a; char b[20]; } x, y; ... main() { char *whocares = "Whoops!"; ... /* Make .a elements equal: */ x.a = y.a = 1; /* Scribble into x.b[0] thru x.b[13]: */ strcpy(x.b,"Bunch of junk"); /* Now x.b[0]='B', x.b[1]='u', ... x.b[13]='\0' */ /* Make .b elements "equal?": */ strcpy(x.b,whocares); /* Write into x.b[0] thru x.b[7] */ strcpy(y.b,whocares); /* Write into y.b[0] thru y.b[7] */ if (x == y) /* it would never get here! */ ... The reason that x is not equal to y is that x.b[8] still contains the ' ', and x.b[9] still contains 'j', and so on from the first strcpy into it, while y.b[8] contains whatever it was initialized to (perhaps '\0') and so on. While it is definitely not impossible for a compiler writer to make a structure comparitor (I think), one wonders about the practicality of it, unless all programmers started clearing every byte of any character array in the structures before reusing them (yuck to the sixth power). Just to get structure comparisons?? [One could just as easily add the following code to the above to make it work: #define wierd_EQ(m,n) ((m).a == (n).a && strcmp((m).b,(n).b) == 0) /* ^^^^^ name of the structure tag, if any (give it one!) */ Then use: if (wierd_EQ(x,y)) or if (! wierd_EQ(x,y)) in place of: if (x == y) or if (x != y) There would be a similar ..._EQ macro for each structure type. Just an idea, anyway.] WooDS (Warren D. Swan)