C++ definition of NULL
Marc Espie
espie@quatramaran.ens.fr
Wed Mar 3 18:32:00 GMT 1999
In article < 199903031616.IAA18283@cygnus.com > you write:
>I am also unhappy about style guides that discourage use of NULL.
I thoroughly discourage use of NULL in code I write, and in any style
guide I would write.
Why ?
Well, I guess this discussion amply proves that NULL is not worth the
trouble. It's only half-portable, compilers hardly agree on what to do
with it, and you have to compile with -ansi to get standard compliance
from egcs.
Yes, I know that what's -ansi does, but this looks a bit like a straitjacket:
-ansi is a choice on a lot of issues, I don't like having this choice taken
away just for using NULL.
Besides that, NULL seems to imply it is magical. It does not hold its promises:
it it just a decorated integer constant, at least on most compilers.
Whereas 0 does not hold any getcha. You know you've got to cast it if you
use it in an ambiguous context. Try showing weird code that uses NULL to
beginners... good luck getting them to understand why it's not portable.
The committee blew it: there should be another constant, a NIL or something,
based on any sane pointer definition, like what Meyer writes at the end
of Effective C++. NULL needs to stay the way it is for compatibility with
legacy code.
But avoiding NULL is just plain common sense. :)
More information about the Gcc
mailing list