Thread: Where's NULL?

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    4

    Where's NULL?

    In a program I'm writing I tried to initialize some pointers to NULL. When I tried to compile, I received the following message:

    error: 'NULL' was not declared in this scope


    The file this is occurring has no #includes. What needs to be imported for NULL to work? Where is the macro defined?

    Thanks

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    It is implementation defined. A logical place to have it defined is probably <cstdlib>.

    Or you can define it yourself with #define NULL 0, or #define NULL 0L, or any other arithmetic integral type.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    In fact, a quick reading of the standards reveals its location to be standard defined. It's in <cstddef>
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    4

    Smile Thanks...

    Thanks for the help.

    ~~Chris

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366

  6. #6
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Why not just use 0?
    Yes, I know it is the equivilent of NULL, but many books advise using assigning NULL to
    a pointer to show it has been safely deleted from allocation with delete.

    Code:
    Foo *fo = new Foo;
    
    delete fo;
    fo = NULL;
    My assumption behind this is that zero '0' appears to be purley integer based,
    where as NULL is better used on pointer operations.

    I am not saying you are wrong daved, but I wonder why many books teach pointer
    allocation to NULL and not zero as you suggested? I am intersted on your view on many
    books teachings.
    Double Helix STL

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    I personally prefer 0.

    MinGW does not even define NULL correctly, adopting instead ((void*)0). For some reason the __cplusplus macro is not defined when reaching stddef.h and the correct C++ form is not used.

    This code, compiled with mingw32-g++.exe, gives me "warning: converting to non-pointer type `int' from NULL"

    Code:
    #include <cstdlib>
    int main() { int p = NULL; }
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  8. #8
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    using NULL or 0 is just preference. It's both correct. The books probably suggest doing it because when you see NULL you immediately see you're assigning to a pointer.

    I dont care if somebody uses 0 or NULL, I usually just adopt the style used by the one before me. when I write things just for me I just use 0, less typing

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> why many books teach pointer allocation to NULL and not zero as you suggested?
    There are a lot of practices suggested by C++ books that are not exactly how I (or Bjarne Stroustrup) would do it. Part of this is because many books use C style or older C++ style idioms and practices in their C++ code. Since NULL is actually different than 0 in C, there is more reason to use it there. As has been said, it is perfectly fine to use in C++, I just prefer 0. And as the link says, we will all be using nullptr in the future anyway.

  10. #10
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    we will all be using nullptr in the future anyway.
    Good. Im glad about that, it is to the point and you can see imediatley what the statement does
    Double Helix STL

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  2. Help with yacc/compiler design/seg fault
    By trippeer in forum C Programming
    Replies: 1
    Last Post: 04-08-2005, 03:43 AM
  3. Wierd Segmentation Faults on Global Variable
    By cbranje in forum C Programming
    Replies: 6
    Last Post: 02-19-2005, 12:25 PM
  4. Big Problem With Passing an Matrix to Main Function
    By Maragato in forum C Programming
    Replies: 4
    Last Post: 06-14-2004, 11:06 PM
  5. Really Need Help With My Opengl Camera!!!!
    By psychopath in forum Game Programming
    Replies: 13
    Last Post: 05-28-2004, 03:05 PM