NULL vs. 0

This is a discussion on NULL vs. 0 within the C++ Programming forums, part of the General Programming Boards category; Why isn't NULL defined as (void*)0 instead of just 0 ? Wouldn't that make everyone happy?...

  1. #16
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Why isn't NULL defined as (void*)0 instead of just 0?
    Wouldn't that make everyone happy?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  2. #17
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,611
    In C++? Honestly?
    void* is not implicitly convertible to T* in C++.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #18
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Quote Originally Posted by laserlight View Post
    NULL might not be 0. For example, it could be 0L. Either way, it evaluates to zero.
    Well, technically NULL doesn't have to evaluate to 0, it has to evaluate to a pointer to absolute memory location 0, which on a segmented machine can be a non-zero value (e.g. DOS). However, every known implementation implements it as 0.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  4. #19
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by abachler View Post
    Well, technically NULL doesn't have to evaluate to 0, it has to evaluate to a pointer to absolute memory location 0, which on a segmented machine can be a non-zero value (e.g. DOS). However, every known implementation implements it as 0.
    You might want to re-read my first post in this thread. Because the standard mandates that NULL == 0. So it would only be possible for non-standard-compliant compilers.

  5. #20
    Registered User
    Join Date
    Apr 2006
    Posts
    2,035
    Quote Originally Posted by EVOEx View Post
    You might want to re-read my first post in this thread. Because the standard mandates that NULL == 0. So it would only be possible for non-standard-compliant compilers.
    No, a null pointer can be any value. However, when compared to another null pointer or the literal 0 (or 0L or something) then it must be equal. When compared to reinterpret_cast<T*>((int)0) then the result is undefined, although all implementations will say it is equal. The point is, C++ does not mandate anything about the implementation representation of pointers.
    Last edited by King Mir; 12-20-2009 at 08:28 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,731
    Quote Originally Posted by abachler
    Well, technically NULL doesn't have to evaluate to 0, it has to evaluate to a pointer to absolute memory location 0, which on a segmented machine can be a non-zero value (e.g. DOS). However, every known implementation implements it as 0.
    Technically, NULL has to evaluate to zero. I quoted the C++ standard in post #3. In the case you described, it would simply be a non-zero value as an address, but it nonetheless evaluates to zero.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #22
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    478
    Code:
    #ifdef _cplusplus
    #define NULL 0
    #else
    #define NULL (void*)0
    #ifdef
    Taken directly out of ..stdio, I think.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

Page 2 of 2 FirstFirst 12
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, 11:25 AM
  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

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