NULL vs. 0

This is a discussion on NULL vs. 0 within the C++ Programming forums, part of the General Programming Boards category; Suppose we have the variable declaration double * ptr; What, if any, is the difference between the assignments: ptr = ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    242

    NULL vs. 0

    Suppose we have the variable declaration double * ptr;

    What, if any, is the difference between the assignments:

    ptr = 0;

    and

    ptr = NULL;

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,988
    There might be no difference at all, since NULL is an implementation defined null pointer constant (that is not (void*)0), and by definition a null pointer constant is "an integral constant expression rvalue of integer type that 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

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by laserlight View Post
    There might be no difference at all, since NULL is an implementation defined null pointer constant (that is not (void*)0), and by definition a null pointer constant is "an integral constant expression rvalue of integer type that evaluates to zero".
    Actually, not according to Stroustrup:

    Should I use NULL or 0?
    In C++, the definition of NULL is 0, so there is only an aesthetic difference. I prefer to avoid macros, so I use 0. Another problem with NULL is that people sometimes mistakenly believe that it is different from 0 and/or not an integer. In pre-standard code, NULL was/is sometimes defined to something unsuitable and therefore had/has to be avoided. That's less common these days.

    If you have to name the null pointer, call it nullptr; that's what it's going to be called in C++0x. Then, "nullptr" will be a keyword.
    Stroustrup: C++ Style and Technique FAQ

  5. #5
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    You can signify an unset/invalid std::function and boost::shared_ptr (and other's as well I'm sure) by setting them 0. Are we then going to be setting them to nullptr? That seems kind of weird since they aren't pointers themselves. If we can't, then we'll be using nullptr in some parts, and NULL/0 in other parts - Yay (sarcasm). I prefer 0 since I think of a pointer as a variable to a point in memory, and 0 as a invalid point (you'd never use). I also think of 0 as a non-successful value (false), versus a positive value (true), so conditionally it makes sense. With all of the technicalities of C/C++ I don't understand why using 0 as null, or even NULL, is a certain and needs replacing. Something set to NULL is obvious, and does the job. Can even macro/typedef your own nullptr, nullfunc, nullobj to 0, or whatever.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,988
    Quote Originally Posted by EVOEx
    Actually, not according to Stroustrup:
    In what way do you think Stroustrup contradicts my statement?

    Quote Originally Posted by Dae
    Something set to NULL is obvious, and does the job.
    The problem is that you can set a non-pointer to NULL, since NULL evaluates to zero. This is precisely what nullptr is supposed to avoid.
    Last edited by laserlight; 12-20-2009 at 11:32 AM.
    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. #7
    Registered User
    Join Date
    May 2009
    Posts
    242
    Trying to summarize:

    1) NULL is just 0 for pointers (but used to have some different, and bad, implementations)
    2) You can always use 0 in place of NULL
    3) Since NULL is a macro, 0 might save some computational time
    4) Stroustrup uses 0
    5) NULL does have a symbolic difference, similar to that between '\0' and 0, but has some rough edges in the correlation (dissimilar to the '\0' correlation to 0).

    That makes it sound awfully tempting to eliminate NULL from one's active vocabulary...

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,988
    Quote Originally Posted by Aisthesis
    1) NULL is just 0 for pointers (but used to have some different, and bad, implementations)
    Well, yes, NULL is a null pointer constant, though "0 for pointers" sounds a little strange.

    Quote Originally Posted by Aisthesis
    2) You can always use 0 in place of NULL
    Yes.

    Quote Originally Posted by Aisthesis
    3) Since NULL is a macro, 0 might save some computational time
    No, other than the time spent by the preprocessor in replacing NULL with the actual null pointer constant, but this is very negligible and does not matter in the end.

    Quote Originally Posted by Aisthesis
    5) NULL does have a symbolic difference, similar to that between '\0' and 0, but has some rough edges in the correlation (dissimilar to the '\0' correlation to 0).
    I am not sure what you mean by this. The advantage of using NULL over 0 is that it is clear that it is a null pointer constant. The disadvantage of using NULL instead of 0 is that this null pointer constant can be used in a context that has nothing to do with pointers (though apparently some compilers may be able to detect such cases and issue warnings).
    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

  9. #9
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Quote Originally Posted by laserlight View Post
    The disadvantage of using NULL instead of 0 is that this null pointer constant can be used in a context that has nothing to do with pointers (though apparently some compilers may be able to detect such cases and issue warnings).
    I'm not really sure this is a disadvantage, although setting anything other than a pointer to NULL would look odd, and a little unprofessional.
    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.

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by laserlight View Post
    In what way do you think Stroustrup contradicts my statement?
    The there "might not" be part didn't sound great to me. More like: there shouldn't be any difference. It sounded like you suggested that NULL might not have the value of 0, as many people seem to think.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,988
    Quote Originally Posted by EVOEx
    The there "might not" be part didn't sound great to me. More like: there shouldn't be any difference. It sounded like you suggested that NULL might not have the value of 0, as many people seem to think.
    NULL might not be 0. For example, it could be 0L. Either way, it 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

  12. #12
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875

    Angels/pins

    Sounds like one of those "How many angels can dance on the head of a pin?" questions....the answer to which is only as valid as someone else's opinion. I tend to agree with laserlight but I am sure that opinion will horrify some of the purists so I am stepping around this particular road apple...

    Happy Sunday all.
    J.
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  13. #13
    Registered User
    Join Date
    May 2009
    Posts
    242
    Quote Originally Posted by laserlight View Post
    I am not sure what you mean by this. The advantage of using NULL over 0 is that it is clear that it is a null pointer constant.
    That's what I meant. You can also use 0 instead of '\0', but the latter indicates the null terminator more clearly.

    Well, that gives NULL a bit of an edge, then. No real downside but greater clarity.

    But it looks like the new release will introduce nullptr if I'm understanding various posts in this thread correctly. No way they'll deprecate NULL, though, pretty much ever. Way too much code already uses it.

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,820
    NULL will stay for backwards compatibility...
    And yes, I would say you can set a smart pointer to nullptr, since it emulates a pointer, hence should be a pointer from your view (and the language's).
    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.

  15. #15
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    I'm surprised this hasn't been mentioned yet.

    The problem with NULL is that it usually defined as 0 and therefore isn't a pointer type. Therefore it is possible to use it in a context as an integers, and worst of all, it can be used in cases where an integer or a pointer is allowed, such as overloaded functions. In such cases it will generally be taken to be an integer. Using 0 instead of NULL in such cases makes this error more apparent, because a programmer reading the code will not be tempted to think that a pointer is being passed.

    For this reason, I suggest using 0 instead of NULL in C++. (But not in C)
    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.

Page 1 of 2 12 LastLast
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