[cfe-dev] Style question: NULL or 0?

Marshall Clow mclow.lists at gmail.com
Wed Aug 17 21:06:35 PDT 2011


On Aug 17, 2011, at 4:41 PM, Jordy Rose wrote:

> Hi, LLVM. I have a question I'd like to get put into the official style guidelines: do we prefer NULL or 0 for C++ objects? I've seen both throughout the code.
> 
> Personally I prefer NULL, since it establishes that something is a pointer and not an integer (or integer-constructed object, but thankfully we avoid implicit conversions in LLVM/Clang). But I think I read somewhere that 0 is more C++esque. (And C++11 nullptr's not available yet, of course.)
> 
> 
> Similarly, when testing for the null pointer, is it better to use:
> 1. (x) and (!x)
> 2. (x) and (x == NULL)  // or 0
> 3. (x != NULL) and (x == NULL)
> 4. (NULL != x) and (NULL == x)
> 5. ...some other combination or something I haven't thought of
> 
> I don't care about this one as much. Still, in this case I think the first is fairly standard, though the second does make the explicit test for NULL stand out a little more (fewer copy/paste errors, perhaps?). IMO (3) and (4) are overkill.


I use #4 most of the time.
Rationale:
* Make it explicit
* Constant on the left hand side.

-- Marshall

Marshall Clow     Idio Software   <mailto:mclow.lists at gmail.com>

A.D. 1517: Martin Luther nails his 95 Theses to the church door and is promptly moderated down to (-1, Flamebait).
        -- Yu Suzuki





More information about the cfe-dev mailing list