Hi,
Sometimes when I use NULL the compiler spits out an error saying NULL is not defined. Could anyone tell me the reason (in general) why this would happen.
Cheers
Alex
Hi,
Sometimes when I use NULL the compiler spits out an error saying NULL is not defined. Could anyone tell me the reason (in general) why this would happen.
Cheers
Alex
Because you haven't #include'd one of the header files (eg <stdlib.h> or <cstdlib>) that #define's NULL.
NULL is a macro which is defined in one of the standard headers (e.g <cstddef>, but that gets included by almost any other std header).
However, since NULL in C++ is defined as 0, you might start using 0 instead of NULL so as not to depend on whether a macro is defined or not.
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
I find it USEFUL to differentiate between NULL and zero - NULL tells me (and anyone else reading the code) that this is comparing a pointer - as opposed to for example something that the pointer was pointing at. But I guess it's a "style" issue - as it won't actually change any behaviour in the code whichever one chooses.
--
Mats
Stroustrup's answer (that I pretty much agree with) is that he prefers 0 since he doesn't like macros, and nullptr will be added to the next standard which is what will become the best choice for assigning a null pointer.
There is no guarantee nor any requirement that any of the standard headers #include <cstddef>. However, in practice, the headers with some compilers do.
NULL is not defined as zero.
A C++ compiler (and a C compiler), by convention, is required to treat a zero valued pointer as if it were the NULL pointer. However, there is no requirement for the reverse to be true.
I thought it treats NULL pointers as zero.treat a zero valued pointer as if it were the NULL pointer.
Learn C++ (C++ Books, C Books, FAQ, Forum Search)
Code painter latest version on sourceforge DOWNLOAD NOW!
Download FSB Data Integrity Tester.
Siavosh K C
Stroustrup begs to differ: in C++, the definition of NULL is 0.NULL is not defined as zero.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
The C++ standard, Section 8.1 para 4 states "The macro NULL is an implementation-defined null pointer constant in this international standard (4.10)".
Section 4.10 states that a "null pointer constant" is an rvalue of integer type that evaluates to zero.
One interpretation of that, incidentally stated in a footnote attached to the statement in Section 8.1 I quote above, is that possible definitions of NULL include 0 and 0L (but not (void *)0).
This is ambiguous. Because zero is an integer. Why it says "evaluates to zero"?Section 4.10 states that a "null pointer constant" is an rvalue of integer type that evaluates to zero.
Learn C++ (C++ Books, C Books, FAQ, Forum Search)
Code painter latest version on sourceforge DOWNLOAD NOW!
Download FSB Data Integrity Tester.
Siavosh K C
The null pointer is a pointer that you must not dereference, but it differs from dangling and uninitialized pointers in that it has a (implementation-) defined value and you can easily determine that it is not pointing to a valid object (by seeing if it == 0 in C++) making it useful to represent a "currently not valid pointer".
It doesn't have to point into memory at address value 0, but for assignments and comparisons it must behave as if it did.
Any usages, such as seeing how far the offset of your array is from NULL and such, result in undefined behaviour. At least, that's how I understand it.
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
That supports anon's and Stroustrup's assertion that the definition of NULL in C++ is 0. You can argue that technically 0L or other integer literals equal to 0 can also be the definition, but surely you cannot argue that the definition of NULL is not 0 (though you can argue that the definition of NULL is not necessarily the int literal 0). Ultimately the point that anon made is that 0 can be used in place of NULL, not that NULL should be used in place of 0.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)