Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watmath!clyde!rutgers!seismo!brl-adm!brl-smoke!gwyn From: gwyn@brl-smoke.UUCP Newsgroups: news.software.b,comp.lang.c Subject: Re: News for Xenix on PC AT ? Message-ID: <5787@brl-smoke.ARPA> Date: Thu, 23-Apr-87 13:09:18 EST Article-I.D.: brl-smok.5787 Posted: Thu Apr 23 13:09:18 1987 Date-Received: Sat, 25-Apr-87 06:44:55 EST References: <18346@ucbvax.BERKELEY.EDU> <145@sds.UUCP> <17005@sun.uucp> <146@sds.UUCP> Reply-To: gwyn@brl.arpa (Doug Gwyn (VLD/VMB) ) Organization: Ballistic Research Lab (BRL), APG, MD. Lines: 56 Xref: utgpu news.software.b:509 comp.lang.c:1796 In article <146@sds.UUCP> dave@sds.UUCP (dave schmidt x194) writes: -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)" ??? Because that's not what the C language specification says happens for such a comparison. -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. No cast is needed for the 0; 0 has special pointer properties in addition to its use as an integer constant. - 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. Another person has made the mistake of contradicting Guy in an area he is expert in. Guy's explanation is EXACTLY RIGHT independent of machine architecture or implementation, including bit pattern used to represent a null pointer (always written as 0 at the C source level regardless of the implementation). He, I, and others have explained more than once before in the C newsgroup that the only fully correct definition of the macro NULL, as C now stands, is as 0. (Under the proposed ANSI standard you could probably get away with defining it as (void *)0.) I will agree that writing if ( !pointer ) is bad style, but it is nonetheless technically correct. If your compiler can't handle this, then it is broken; perhaps you should try to get it fixed.