Is NULL valid in C++?

This is a discussion on Is NULL valid in C++? within the C++ Programming forums, part of the General Programming Boards category; I read on this other forum a member saying that NULL does not exist in C++ and that one might ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    204

    Is NULL valid in C++?

    I read on this other forum a member saying that NULL does not exist in C++ and that one might get a compiler error using it. Is it true? Isn't NULL just the value 0, just like in C? Thanks.

  2. #2
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    NULL is not a built-in type, but is a macro and hence can be defined as anything. I believe that the standard does require that when pointers are assigned NULL that NULL translate to a guarenteed invaild pointer (on most machines that would in fact be zero, but not on all machines). NULL is also a macro in C, so you can't guarenteed that it will behave either, though I suspect the standard calls for the same or similiar results as for C++.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    > Isn't NULL just the value 0, just like in C?
    It should be.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203
    I think NULL isn't C++.
    One has to include some headerfile which defines it....
    Any way I'm not sure...

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    5,889
    NULL is a value (often a macro) defined in some standard C headers (eg stdlib.h), and is available in C++ if you #include those headers. If you don't #include a header that defines NULL, a compiler error will result (in both C and C++) if you use it.

    NULL is not required to be the value zero, in either C or C++. What is required is that the assignment "x = 0;", where x is a pointer to Type, is required to have the same effect as "x = (Type *)NULL". In practice, that often means that NULL has a value zero, but (as mitakeet says) there are some machines where that is not true.

    Note: the above is definitely true of the 1989 C standard and the C++ standard. I haven't checked if it is true of the 1999 C standard.

  6. #6
    *this
    Join Date
    Mar 2005
    Posts
    498
    I thought NULL was ((void*)0)

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    taken from malloc.h

    Code:
    #if defined(__STDC__) || defined (__cplusplus)
    # include <stddef.h>
    # define __malloc_ptr_t  void *
    #else
    # undef  size_t
    # define size_t          unsigned int
    # undef  ptrdiff_t
    # define ptrdiff_t       int
    # define __malloc_ptr_t  char *
    #endif
    
    #ifndef NULL
    # ifdef __cplusplus
    #  define NULL  0
    # else
    #  define NULL  ((__malloc_ptr_t) 0)
    # endif
    #endif
    signature under construction

  8. #8
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    As mentioned before, NULL is OS and compiler dependant, so showing a section of malloc.h without specifying that information is useless.

    On MOST hardware/OS/compiler combinations that MOST of the people browsing this forum are going to encounter zero is the expected value for NULL. When it isn't zero for your hardware/OS/compiler using zero instead of the macro will get you into a world of hurt.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  9. #9
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    well i just meant:
    in these headers NULL is defined explicitely for c++
    also, when you look at c++ headers, you see that they also use NULL

    edit:
    so independed of wheter youre using c or cpp, both use pointers (where c++ references are pointers that behave syntactically as if they were the object itself)
    so whenever you have a pointer there must be a NULL value
    (of course there would be the option:
    Code:
    struct Pointer {
    bool is_p_pointing_to_a_valid_location;
    void *p;
    };
    but that would waste one byte per pointer.
    thus its more useful of reserving ONE byte at some address to be the NULL address (e.g. 0x00000000. of course it could also be 0x12345678 or 0xffff).
    Last edited by Raven Arkadon; 06-29-2005 at 09:05 AM.
    signature under construction

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >When it isn't zero for your hardware/OS/compiler using zero instead of the macro will get you into a world of hurt.
    No, it really won't: http://www.research.att.com/~bs/bs_faq2.html#null

  11. #11
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,607

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with yacc/compiler design/seg fault
    By trippeer in forum C Programming
    Replies: 1
    Last Post: 04-08-2005, 03:43 AM
  2. Wierd Segmentation Faults on Global Variable
    By cbranje in forum C Programming
    Replies: 6
    Last Post: 02-19-2005, 11:25 AM
  3. 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
  4. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 06:48 PM
  5. BST/Red and Black Tree
    By ghettoman in forum C++ Programming
    Replies: 0
    Last Post: 10-24-2001, 10:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21