Thread: NULL and 0

  1. #1
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895

    NULL and 0

    I just read in the time(0) thread that 0 is to be preferred over NULL in C++.

    What is the reasoning for this? I'm all for it, but I might need to convince others.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  2. #2
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    If I remember correctly, its because NULL isn't automatically defined as zero, even though most of the time it is. If a compiler wanted to define NULL as -13 it can, which is why it's simply safer to just use zero when that is what you want.

  3. #3
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    NULL can be defined as:
    Code:
    #define NULL 0
    or
    Code:
    #define NULL ((void*)0)
    so you can always use 0 when a pointer is required, but not the other way around.

    as for the time function:
    http://www.cppreference.com/stddate_details.html#time
    Code:
    time_t time(time_t* time);
    it takes a pointer, so you can use any of 0 or NULL.

    EDIT: missed ()
    Last edited by glUser3f; 12-04-2003 at 03:27 PM.

  4. #4
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    Originally posted by PJYelton
    If I remember correctly, its because NULL isn't automatically defined as zero, even though most of the time it is. If a compiler wanted to define NULL as -13 it can, which is why it's simply safer to just use zero when that is what you want.
    0 is used as NULL because almost every OS uses the first memory address for its own stuff, so a program can use it to indicate an invalid address.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I was referring to this particular comment by Prelude:
    In C++ the use of the macro NULL is not recommended, so the integer literal 0 is used because in pointer context it is treated as a null pointer.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    NULL is a macro that in C is usuallly either 0 or ((void *)0). in C the cast makes sense, as you can always assign a void* to any pointer type without an explict cast, but not to an int (or float for that matter). In some C implementations (and this may still be valid) NULL would have a numeric value other than zero so NULL is prefered. In C++ 0 the literal is magical and part of the language. If you are on a platform where you need some other binary value for an invalid address it is the resposibility of the language to provide that value. Same deal with an implied cast to a bool. while(p->next) p = p->next; Could break in K&R C on oddball wierdo platforms where NULL is non-zero, in C++ the language makes sure that a pointer to NULL evaluates to false. This may also be true of C99 and later.

    type *p=0; // always cool in C++
    (int)p == 0; // MAY NOT BE TRUE IN C++
    if(p==0 && !p) // always true in C++

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Got it, thanks!
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed