int main ()
{
int n = NULL;
return 0;
}
#define NULL 0 // C++ definition.
#define NULL ((void*)0) // C definition.
Edit: I think the definition is not supposed to have a semicolon.Correct. That will make the semicolons themselves be part of the text replace the preprocessor performs. That aside id also use a global const int and not a define. Then again I'm also anal retentive about type issues. =-)
I think the definition is not supposed to have a semicolon.Yes you are right: edited. Thanks for pointing it out.
just never use NULL? it is not standard C/C++.
Why would you use it, it is just not professional.
If we want to be stupid and use NULL, well then we can as well use ZERO, now let's hope our compiler 'defines' it for us.
Bweurk, #define, yet another non professional thing ...
NULL is standard. If you do not believe me, buy the standard.In case anyone is wondering:
-4- The macro NULL is an implementation-defined C++ null pointer constant in this International Standard (conv.ptr).*
[Footnote: Possible definitions include 0 and 0L, but not (void*)0. --- end foonote]
The macro NULL is an implementation-defined C++ null pointer
nullptr - C++0x keyword for the null pointer. It is not an integer. It can be assigned only to pointers.
The trouble was that NULL would be defined in various ways on different platforms :
#define NULL ((char*)0)
#define NULL ((void*)0)
#define NULL 0
These various definitions wreaked havoc with portability of C++ programs [My note : As said already above]
In fact there is no way to represent a null pointer directly in C++, but we're guaranteed that the numeric literal 0 in convertible to a null pointer. That is what C++ programmers traditionally used to ensure the portability and correctness of their code. Now the standard indicates that definitions like (void*)0 are not allowed.
d also the C++ language; and its use is professional: it makes software more readable, less ambiguous and more maintainable.Yes really, it breaks code. i wonder why Dewhurst called his topic 'Bad language'.
If we look at the code we want to see immediately if something is a "null pointer constant" or the zero integer.This brings nothing :
#define nullptr 0Suggestion : put it in your personal namespace so you don't get any conflicts. Once the C++0x is there you can replace all MyNameSpace::nullptr by nullptr. So make a constant out of it instead of a define. And that constant lives in its namespace, your define is now everywhere and can fight with other doing a similar thing.
Implementation-defined !!!! --> NOT standard.
It shows up in so many headers, why ... because it is not standard.
People just define it in headers because they are used to using it.
I have been attending many talks of Mr Stroustroup where he is stressing it is not standard
[maybe just for the fact that it is not portable], in his code samples he writes 0.
In fact it is a real violation of strong types.
If it is so standard, why are there so many compilers who do NOT define it, some do it in stdio.h others in stdlib.h, correction : cstdio and cstdlib. Trust me, I have been using a lot of compilers and I have seen code break because of the lovely NULL not being defined.
Darn right let's include a huge header file to get the NULL define, and on the other hand we pimpl and other stuff to get less include dependencies, but let's break all that for that nice lovely NULL.
If I recall correctly, they are thinking about a *template* for something like nullptr, but I am not sure.
For sure it will be a keyword --> so part of the language :
a define is not part of the language, it is the same story that some compilers have a define for PI and others don't.
And I have heard/read similar things from Sutter, Meyers about NULL not being standard.
Wrapping up : just avoid using NULL, as said it is not professional and makes your code break on different platforms.
In section 18 (Language support library), subsection 1 (Types), it mandates the existence of a macro, defined in <cstddef>, whose identifier is "NULL" and whose replacement list is implementation-defined. NULL is "standard",
[...] bullfeces [...] crap [...] madness [...] crap.
Whatever I say tends to come out is if it used the wrong end...
I #define nullptr 0 for the time being. It's being added to the C++ standard in the next revision as something different than 0 to give a little more safety to null pointers, so it's probably a better way to do it for now. Just remember to remove the #define when C++0x hits.What is wrong with static const void *nullptr = 0;? It obviously won't do for foo_t* bar = nullptr; without a cast, but if(foo == nullptr) should do just fine, and would bark if it encounters pointer/integral mismatches.
If I recall correctly, they are thinking about a *template* for something like nullptr, but I am not sure.You're going to laugh... for some reason, I woke up this morning and had an implementation of nullptr in my mind.