Thread: NULL vs. 0

  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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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,195
    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.

  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,149
    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 09: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
    28,413
    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.
    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

  7. #22
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    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."

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