[LLVMdev] Style question: NULL or 0?

Jordy Rose jediknil at belkadan.com
Wed Aug 17 16:41:35 PDT 2011


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.


Thoughts?
Jordy



More information about the llvm-dev mailing list