Thread: difference between 0 and null

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    17

    difference between 0 and null

    Hi

    What's the difference between these two statements - I'm not sure, and I'm using one of them in srand():

    time(0)

    time(NULL)

    Thanks

  2. #2
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    There is no difference. NULL is defined as 0.

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    NOOOOOOOOOO!

    NULL is a macro... when you use time(0), it's expecting a pointer, so when it gets a 0, it kinda sends the data nowhere... but when you pass NULL, the compiler defines NULL as what it wants... usually it is 0, but it can also be something else...

    NULL != 0
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >NOOOOOOOOOO!
    You're overreacting just a smidge.

    >NULL is a macro...
    Good so far

    >when you use time(0), it's expecting a pointer
    Still good

    >so when it gets a 0, it kinda sends the data nowhere...
    Eh? When 0 is used in a pointer context, it is converted to a null pointer. Note that a null pointer need not be all bits zero, even when the literal 0 is used.

    >but when you pass NULL, the compiler defines NULL as what it wants...
    Technically, NULL is defined well before you use it. However, it is true that NULL may not be all bits zero, the representation of a null pointer is up to the implementation, as is the definition of NULL.

    >NULL != 0
    NULL != 0 when NULL is not defined as 0. Most modern implementations define NULL like so:
    Code:
    #define NULL 0
    However, the use of NULL is considered unsafe practice in C++ because it could also be defined as
    Code:
    #define NULL ((void*)0)
    This causes a world of trouble, while the literal 0 is guaranteed to work correctly.
    My best code is written with the delete key.

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    ^ I've always been told that getting into the habit of saying that "NULL is the same as 0" is as bad as writing void main()...

    I reacted like that because the first response was short and (from what I've been taught) was exactly the wrong answer to give...
    Last edited by major_small; 12-10-2003 at 11:03 AM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I've always been told that getting into the habit of saying that "NULL is the same as 0" is as bad as writing void main()...
    That attitude is a little bit extreme. void main is explicitly stated by the standard to be ill-formed. A null pointer constant is any expression that evaluates to zero, and NULL is an implementation-defined macro that is highly likely to be an expression that evaluates to zero. So "NULL is the same as 0" is likely to be correct, while writing void main is almost never correct. As with everything, there are no hard and fast rules that apply in all cases. For example, even void main can be legal C++ in certain situations.
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Feb 2002
    Posts
    114
    #define NULL (void*)0

  8. #8
    Registered User cyberCLoWn's Avatar
    Join Date
    Dec 2003
    Location
    South Africa
    Posts
    124
    Maybe a dumb question, but wouldn't (void*)0 give you 0 in any case?

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > but wouldn't (void*)0 give you 0 in any case?
    Whilst (void*)0 is freely usable as a NULL pointer in C (because C treats void* pointers as special), it presents no problem.

    However, having (void*)0 as NULL in C++ raises a lot of needless warnings about casting between pointer types. Since C++ (in a pointer context) converts 0 into (whatever_type*)0 for you, there is no point in decorating
    #define NULL 0
    with
    #define NULL (void*)0
    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.

  10. #10
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    I use NULL all the time, especially for clearing flags (my flags mostly). Its just clearer to read that you're setting that value to a non-accepted value. I use zero only for numbers, null for pointers and flags.

  11. #11
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    NULL the macro is even worse for flags. (bits & NULL) is fine where NULL is 0 but where NULL is ((void *)0) the results are at best an error in C++, quite possibly an implementation defined value even equivilent to (bits & 0xFFFFFFFF) in C Thus it could be exactly the oppisite of what you wanted. 0 in C++ is automagical, as is evaluating a pointer in a boolean context. Years ago while(p && p->next) p = p->next; could fail, today in C++ use of NULL has all of the problems that the original implied cast to int had in C. The problem is much more rare as to be using C headers, yet compiling as C++, and those headers need to be somewhat unaware of C++, and you need (int)(NULL) != 0, and that's excedingly rare, but if you are going to pick nit's NULL is more nitworthy than 0

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Years ago while(p && p->next) p = p->next; could fail,
    News to me - I've never seen that one before
    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.

  13. #13
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    In C, prior to ansi if NULL was non-zero when cast as an int the loop could continue forever. if() while() etc took an int and evaluated true if non zero, C++ takes a bool where ints are cast to true if non-zero and pointers are cast to true if non-null.

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The internal representation of a NULL pointer has always been irrelevant at the level of C source code.

    if ( p )
    if ( p != 0 )
    if ( p != NULL )
    are all the same whether the internal representation of NULL is all bits zero or not.

    Refresher course
    http://www.eskimo.com/~scs/C-faq/s5.html

    Sounds to me more like a case of crappy coding on a DOS machine where it really didn't matter whether you were any good at allocating memory or not. For a while at least, it usually worked anyway.
    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.

  15. #15
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    NULL and 0 : the former is used as a visual cue that something is null (invalidated, negated, denied) whereas the latter might indicate that something is simply integrally zero (though it's perfectly clear what (p!=0) means when p is a pointer, of course).
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed