Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!rutgers!dayton!rosevax!sds!dave From: dave@sds.UUCP (dave schmidt x194) Newsgroups: news.software.b,comp.lang.c Subject: Re: News for Xenix on PC AT ? Message-ID: <146@sds.UUCP> Date: Wed, 22-Apr-87 14:57:40 EST Article-I.D.: sds.146 Posted: Wed Apr 22 14:57:40 1987 Date-Received: Sat, 25-Apr-87 00:59:54 EST References: <18346@ucbvax.BERKELEY.EDU> <145@sds.UUCP> <17005@sun.uucp> Organization: SciCom Data Services, Minnetonka, MN Lines: 63 Xref: mnetor news.software.b:528 comp.lang.c:1860 In article <17005@sun.uucp>, guy%gorodish@Sun.COM (Guy Harris) writes: > > [... commentary on problems w/ news s/w ...] also present > > are things like: > > charptr = malloc(1024); > > if ( !charptr ) ... /* this should be (charptr != (char *)NULL) */ > > This last one falls under the heading of "stupid problems from > [censored] compiler writers who didn't understand C before inflicting > something that they claim is a C compiler on Xenix users." > > if (!charptr) ... > > is IDENTICAL in meaning to > > if (charptr == 0) ... > > which, in turn, is IDENTICAL in meaning to > > if (charptr == (char *)0) ... > > if "charptr" is of type "char *". Why is "if (charptr == 0)" NOT identical to "if ((int)charptr == 0)" ??? This would not work on a machine where sizeof(char *) > sizeof(int). In fact, since we are comparing apples (char *'s) to oranges (int's), a cast is called for and a good, conscientious programmer would perform the cast themself. Furthermore, "if (!charptr)" is not correct on all architectures: I've heard tell of an implementation of C where NULL was 0xffffffff -- the simple "if (!charptr)" fails in this case. Coding the above as "if (charptr == (char *)NULL)" saves you from the "queer machine"; it is also more correct in that it explicitly states what you desire and is more portable. Actually, if NULL is *correctly* defined in stdio.h as "(char *)0" (NULL being the value that malloc() returns on error, NULL *should* have type char *), this could be written as "if (charptr == NULL)". Unfortunately for me, XENIX correctly defines NULL, leaving me to deal with statements such as: int cnt; char ch, *ptr; ptr = NULL; /* this is ok */ ... ch = NULL; /* expands to: ch = (char *)0; */ /* ch = '\0'; is desired */ ... if (cnt == NULL) /* expands to: if (cnt == (char *)0) */ /* i'd settle for if (!cnt) */ ... Given my #define for NULL, statements of this kind make the compiler complain (as it should). That point aside, it makes little sense to me to say that a pointer to a character and a character may be assigned the same value without a cast someplace. This implicitly assumes that NULL is #defined as 0, which is an assumption that good programmers (or should I say "programmers that I regard as good"?) don't make. Dave Schmidt (A firm believer in defensive programming.)