Pointer if test for for NULL

Peter Dufault dufault at hda.com
Thu Apr 29 10:29:27 UTC 2010


On Apr 29, 2010, at 6:20 , Peter Dufault wrote:

> I tried to decide if on a "NULL not all-bits-zero" architecture you could come up with devious prototypes for malloc() to make your example not work.  I guess that's your basic question.  If the NULL bit pattern is actually 0xffffffff and you prototype malloc() to be uint8_t and you ignore all the warnings you're going to get when malloc() returns NULL, then p would wind up being 0x000000ff, and so "if (p)" would succeed.  I don't think this is an argument for removing "if (p) {".  If the style guide for a code base says "use if (p != NULL) {" then by all means change it in *that code base only*, leave it alone in external code.

Sigh.  I left out one of my important points, which is that "if (p != NULL) {" would also test true on the above-mentioned architecture, and I couldn't come up with a scheme where they would evaluate differently.  I've got to let that coffee take effect before posting.

Peter
-----------------
Peter Dufault
HD Associates, Inc.      Software and System Engineering




More information about the users mailing list