4 ms·
The topic was modern C and in modern C environment NULL is defined as (void *)0 There is no point in writing longer form and it's still clearer and safer
by przemoc 12y ago
The topic was modern C and in modern C environment NULL is defined as
(void *)0
There is no point in writing longer form and it's still clearer and safer than 0 alone.
C++ is another story with its
void* hate
built-in. In this land you rather write
(type*)0
or
(type*)NULL
(or
static_cast<type*>(0)
for extra purists), but as you're denoting pointer type already in this notation, there is not much gain in using NULL instead of 0 (well, beside grepability).
In many cases you can be done with 0 alone in C++, that's true, and in such cases NULL at least poses some intention, but if you're not careful enough, you may end up putting NULL alone (without pointer-to-type cast) in some variadic function and things start to blow up all of a sudden (that is if your NULL integer width isn't the same as pointer width). That's why having a habit of writing
(type*)0
is a good thing in this land.
Regarding musl check also:
http://git.musl-libc.org/cgit/musl/commit/include/stddef.h?id=c8a9c221 http://git.musl-libc.org/cgit/musl/commit/include/stddef.h?i...
In short, musl's stddef.h has following lines:
#ifdef __cplusplus
#define NULL 0L
#else
#define NULL ((void*)0)
#endif
NULL defined as 0L for C++ is nice workaround, but it works only for LP64 platforms.
In the same vein for Windows C++ x64 environment you need NULL to be 0LL, as it is LLP64 platform.