4 ms·
The original paster didn't say that a null pointer was incorrect, merely that using NULL in C++ was. Which, in recent versions of C++ it certainly is. The corre
by merijnv 10y ago
The original paster didn't say that a null pointer was incorrect, merely that using NULL in C++ was. Which, in recent versions of C++ it certainly is. The correct way to specify a null pointer in C++ is 'nullptr', NULL has been deprecated because it's definition '(void*)0' leads to typing issues for templates.
- filmor 10y agoIn C++, `NULL` is defined as a simple 0 without the `void*` cast.
- CJefferson 10y agoNo, that's exactly wrong (in this case). For variadic C functions, you MUST use 0 (or NULL, which is the same as 0). Passing nullptr will case abort() to be called (as is the case whenever you pass a C++ class into a variadic function).
- CJefferson 10y agoReply (to myself, based on a correction) Apologises, I mis-remembered the rule -- it is only C++ types which have a non-trivial destructor which cause an abort when you pass them to a variadic funciton. HOWEVER. This is still undefined behaviour. nullptr is an object of type std::nullptr_t. When you pass it to execlp, it just gets pushed through as a bit-pattern (as with any type passed to a variadic C function), and there is no guarantee what comes out looks like a "true" (void*)0 C-style null pointer.
- ben0x539 10y agoThere isn't a guarantee that NULL has a bit pattern that's the same as a true (void)0 C-style null pointer either. I didn't look it up directly but apparently NULL is allowed to be nullptr instead of 0, and nullptr converts to void in varargs arguments. I think C/C++ also allow actual null pointers to have different bit patterns than the integer zero, so in C++03 passing NULL to terminate a varargs argument list would technically be incorrect, or maybe implementation-defined.
- CJefferson 10y agoDamn, there is a special exception in the definition of vararg functions, just for nullptr! throws things in the air I give up having discussions about C++.
- ben0x539 10y agoI give up too, because the HN formatting is actively fighting me.