Thread: Null Not Defined

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    45

    Null Not Defined

    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

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Because you haven't #include'd one of the header files (eg <stdlib.h> or <cstdlib>) that #define's NULL.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by anon View Post
    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).
    There is no guarantee nor any requirement that any of the standard headers #include <cstddef>. However, in practice, the headers with some compilers do.
    Quote Originally Posted by anon View Post
    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.
    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.

  7. #7
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    treat a zero valued pointer as if it were the NULL pointer.
    I thought it treats NULL pointers as 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

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    NULL is not defined as zero.
    Stroustrup begs to differ: in C++, the definition of NULL is 0.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by laserlight View Post
    Stroustrup begs to differ: in C++, the definition of NULL is 0.
    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).

  10. #10
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Section 4.10 states that a "null pointer constant" is an rvalue of integer type that evaluates to zero.
    This is ambiguous. Because zero is an integer. Why it says "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

  11. #11
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by laserlight View Post
    Stroustrup begs to differ: in C++, the definition of NULL is 0.
    Well I know who I'm believing.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by grumpy View Post
    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).
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 05:48 PM
  2. Memory leaks problem in C -- Help please
    By Amely in forum C Programming
    Replies: 14
    Last Post: 05-21-2008, 11:16 AM
  3. Why am I getting these errors??
    By maxthecat in forum Windows Programming
    Replies: 3
    Last Post: 02-03-2006, 01:00 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. linked list problem
    By kzar in forum C Programming
    Replies: 8
    Last Post: 02-05-2005, 04:16 PM