references to dereferenced null pointers
Karl Heuer
karl at haddock.ima.isc.com
Fri Mar 16 10:59:48 AEST 1990
In article <12347 at smoke.BRL.MIL> gwyn at brl.arpa (Doug Gwyn) writes:
> #define NULL ((void*)(1-1))
> #define NULL 0L
> #define NULL ((void*)0)
> #define NULL 0
>[All of the above are legal, though] the first example is silly. I
>personally recommend the last example, which (despite some arguments you will
>get from certain IBM PC implementors) is always a correct way to define NULL
>and is simpler than the others. However, the next-to-last example does have
>one advantage, namely that it will cause diagnostics to be generated for more
>instances of abuse of the NULL macro than will the last example.
Another idea I've been toying with is
#define NULL __builtin_NULL
where __builtin_NULL is a keyword that, in a pointer context, acts like a
properly-typed null pointer constant (i.e. just like `0' does), and in a non-
pointer context, causes a diagnostic to be issued. This is "even better" than
the ((void *)0) definition since it should catch *all* abuses of the macro,
though it does of course depend on having a hook in the compiler.
Strictly speaking, NULL is supposed to be defined as 0 or a casted 0, but I
think this would be legal by the as-if rule. (Since no correct program could
tell the difference.)
(It is perhaps worth mentioning again that none of this has anything to do
with the internal representation of a null pointer, which may or may not have
all bits zero.)
Karl W. Z. Heuer (karl at ima.ima.isc.com or harvard!ima!karl), The Walking Lint
More information about the Comp.std.c
mailing list