Assigning NULL to a variable (not pointer)

Discussion in 'C++' started by leonm54@gmail.com, Oct 16, 2006.

  1. Guest

    I remember that this is a bad practice, but can not find a definitive
    resource that states it is bad and why. Can anyone help?
     
    , Oct 16, 2006
    #1
    1. Advertising

  2. wrote:
    > I remember that this is a bad practice, but can not find a definitive
    > resource that states it is bad and why. Can anyone help?


    <devilsadvocate>

    What if nobody besides you think it's a bad practice? And shouldn't
    the sheer fact that you can't find any definitive source stating it's
    bad tell you that it might not be all that bad? Just asking...

    </devilsadvocate>

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Oct 16, 2006
    #2
    1. Advertising

  3. Leon posted:

    > I remember that this is a bad practice, but can not find a definitive
    > resource that states it is bad and why. Can anyone help?



    Imagine two different planets with two totally different species, two
    totally different cultures. These two planets don't communicate with each
    other as they are unaware of each other's existance.

    Give each of them the C++ Standard and a compiler.

    One planet will come up with its own ideals and styles, and the other
    planet will come up with different ones.

    If your question is to do with C++ itself, then the answer is simply this:
    Using NULL for zero is harmless, and your compiler won't complain.

    If your question is to do with the culture of C++ which exists presently on
    Earth, then the answer is: We tend to use NULL only for pointers... and
    it's quite irregular to use NULL for integers.

    So if you want to fit in, don't use NULL for integers.

    If you want to use NULL for integer zero, then go ahead, your compiler
    won't complain.

    I myself never use NULL.

    --

    Frederick Gotham
     
    Frederick Gotham, Oct 16, 2006
    #3
  4. wrote:

    > I remember that this is a bad practice, but can not find a definitive
    > resource that states it is bad and why. Can anyone help?


    NULL evaluates to a integer constant with value 0. If you want to assign 0,
    using 0 is a lot more clear. And if you use 0 not by the numeric value but
    as some error/no error flag, the following will be more expressive:

    const int MyFuncNoError= 0;

    int myfunc ()
    {
    // ...whatever
    return MyFuncNoError;
    }

    if (myfunc () == MyFuncNoError)

    Using NULL you just confuse people by making it to think that myfunc returns
    some type of pointer.

    Some people use NULL for '\0', probably confusing it with the NUL name of
    the char in ascii. This also causes a lot of confusion:

    if (somestr != NULL && * somestr != NULL)

    Looking at this you can think that somestr is a char * * instead of a char
    *.

    In a sentence: NULL is intended to be used with pointers, if you use it to
    other things you just add confusion and get no benefit.

    --
    Salu2
     
    =?ISO-8859-15?Q?Juli=E1n?= Albo, Oct 16, 2006
    #4
  5. Guest

    First, thats for the replies to this query.

    I thought I remembered that in some systems that NULL is not '\0'.
    I know that in the majority of systems NULL can equate to '\0', but
    I thought there were exceptions and did not what to make any
    assumptions.

    Not only that, I thought that the definition of NULL was (at least in
    some instances): ((void *)0). If one attempted to assign that to an
    integer, you would at least get a casting error.

    If either the above statements are true, I would think that doing
    something like "int var = NULL;" would generally be a bad idea.


    Frederick Gotham wrote:
    > Leon posted:
    >
    > > I remember that this is a bad practice, but can not find a definitive
    > > resource that states it is bad and why. Can anyone help?

    >
    >
    > Imagine two different planets with two totally different species, two
    > totally different cultures. These two planets don't communicate with each
    > other as they are unaware of each other's existance.
    >
    > Give each of them the C++ Standard and a compiler.
    >
    > One planet will come up with its own ideals and styles, and the other
    > planet will come up with different ones.
    >
    > If your question is to do with C++ itself, then the answer is simply this:
    > Using NULL for zero is harmless, and your compiler won't complain.
    >
    > If your question is to do with the culture of C++ which exists presently on
    > Earth, then the answer is: We tend to use NULL only for pointers... and
    > it's quite irregular to use NULL for integers.
    >
    > So if you want to fit in, don't use NULL for integers.
    >
    > If you want to use NULL for integer zero, then go ahead, your compiler
    > won't complain.
    >
    > I myself never use NULL.
    >
    > --
    >
    > Frederick Gotham
     
    , Oct 16, 2006
    #5
  6. wrote:

    [First, please avoid top-posting]

    > First, thats for the replies to this query.
    >
    > I thought I remembered that in some systems that NULL is not '\0'.
    > I know that in the majority of systems NULL can equate to '\0', but
    > I thought there were exceptions and did not what to make any
    > assumptions.


    In C++, NULL is *always* a constant integer expression with the value of
    zero. The most common definition of NULL that I've seen is simply:

    #define NULL 0

    > Not only that, I thought that the definition of NULL was (at least in
    > some instances): ((void *)0). If one attempted to assign that to an
    > integer, you would at least get a casting error.


    Not in C++. In C, it is legal for the implementation to define NULL as
    (void*)0.

    > If either the above statements are true, I would think that doing
    > something like "int var = NULL;" would generally be a bad idea.


    "int var = NULL;" *is* bad when considering the generally accepted
    conventions. However, there is nothing in the language that makes "int
    var = NULL;" illegal. Given that statement, any C++ compiler has to
    initialize var to zero.


    --
    Clark S. Cox III
     
    Clark S. Cox III, Oct 16, 2006
    #6
  7. wrote:

    > I thought I remembered that in some systems that NULL is not '\0'.


    NULL is never '\0', NULL is 0. If you assign 0 to a char the char gets the
    value '\0' by means of habitual conversions, nothing related with NULL
    itself.

    > Not only that, I thought that the definition of NULL was (at least in
    > some instances): ((void *)0).


    Never in standard C++. Some C compilers do it that way.

    --
    Salu2
     
    =?ISO-8859-15?Q?Juli=E1n?= Albo, Oct 16, 2006
    #7
  8. Julián Albo wrote:
    > wrote:
    >
    >> I thought I remembered that in some systems that NULL is not '\0'.

    >
    > NULL is never '\0', NULL is 0. If you assign 0 to a char the char gets the
    > value '\0' by means of habitual conversions, nothing related with NULL
    > itself.


    NULL could indeed be '\0', but that would have no effect on any
    conforming program anyway.


    --
    Clark S. Cox III
     
    Clark S. Cox III, Oct 16, 2006
    #8
  9. Ron Natalie Guest

    wrote:
    ..
    >
    > I thought I remembered that in some systems that NULL is not '\0'.
    > I know that in the majority of systems NULL can equate to '\0', but
    > I thought there were exceptions and did not what to make any
    > assumptions.


    There's no reason why '\0' isn't a valid NULL.

    >
    > Not only that, I thought that the definition of NULL was (at least in
    > some instances): ((void *)0). If one attempted to assign that to an
    > integer, you would at least get a casting error.


    It can be that in C, it's not allowed in C++.
     
    Ron Natalie, Oct 16, 2006
    #9
  10. Guest

    First, again thank you for your posts.

    I believe I got the point about NULL.

    Now I ask a favor. I made the mistake of using my GMAIL account to
    setup my Google Groups account and the bloody thing dumped my email
    address in the postings. I have since taken care of the issue for my
    posting, but the responses posted still contain my email. Could you
    please delete your postings so that the email sniffers won't find me?
    Thank you in advance.

    LM


    Clark S. Cox III wrote:
    > Julián Albo wrote:
    > > wrote:
    > >
    > >> I thought I remembered that in some systems that NULL is not '\0'.

    > >
    > > NULL is never '\0', NULL is 0. If you assign 0 to a char the char gets the
    > > value '\0' by means of habitual conversions, nothing related with NULL
    > > itself.

    >
    > NULL could indeed be '\0', but that would have no effect on any
    > conforming program anyway.
    >
    >
    > --
    > Clark S. Cox III
    >
     
    , Oct 16, 2006
    #10
  11. Somebody posted:

    > I thought I remembered that in some systems that NULL is not '\0'.



    It's perfectly acceptable that NULL would not be defined as the following
    on a conforming implementation:

    #define NULL '\0'

    Here are some other legal definitions of it:

    #define NULL 0
    #define NULL (0)
    #define NULL (5-5)
    #define NULL (27%4-3)


    > I know that in the majority of systems NULL can equate to '\0'



    NULL _must_ compare equal to '\0'.

    if (NULL != '\0') cout << "This is a K++ compiler.";


    > , but I thought there were exceptions and did not what to make any
    > assumptions.



    Make those assumptions, because there are no exceptions.


    > Not only that, I thought that the definition of NULL was (at least in
    > some instances): ((void *)0). If one attempted to assign that to an
    > integer, you would at least get a casting error.


    Try to compile the following with a C++ compiler.

    #define NULL ((void*)0)

    int main()
    {
    int *p = 0;
    }

    Your compiler should explain to you why NULL is _never_ defined in that
    way.

    > If either the above statements are true, I would think that doing
    > something like "int var = NULL;" would generally be a bad idea.



    Well, neither of them _are_ true, so your every implementation should have
    no problem with:

    int var = NULL;


    > NULL is never '\0', NULL is 0. If you assign 0 to a char the char gets
    > the value '\0' by means of habitual conversions, nothing related with
    > NULL itself.



    The character literal, '\0' _always_ evaluates to integer zero. This is how
    we can write such portable code as:

    void strcpy(char *to, char const *from)
    {
    while (*to++ = *from++);
    }

    , therefore it's _not_ just habitual conversion.

    NULL must be an integer compile-time constant which evalutes to zero. '\0'
    must also be zero.

    Therefore, NULL must compare equal to '\0', and furthermore, an implementor
    may define NULL as '\0'.

    --

    Frederick Gotham
     
    Frederick Gotham, Oct 16, 2006
    #11
  12. Default User Guest

    wrote:

    > First, again thank you for your posts.
    >
    > I believe I got the point about NULL.
    >
    > Now I ask a favor. I made the mistake of using my GMAIL account to
    > setup my Google Groups account and the bloody thing dumped my email
    > address in the postings. I have since taken care of the issue for my
    > posting, but the responses posted still contain my email. Could you
    > please delete your postings so that the email sniffers won't find me?
    > Thank you in advance.


    The majority of people here don't use GG and can't delete the postings.
    In fact, none of the people responding in this thread besides you use
    it. Some newsreaders are able to issue cancel messages, those might or
    might not help your situation.





    Brian
     
    Default User, Oct 16, 2006
    #12
  13. Frederick Gotham posted:


    > Try to compile the following with a C++ compiler.
    >
    > #define NULL ((void*)0)
    >
    > int main()
    > {
    > int *p = 0;
    > }



    int *p = NULL;

    --

    Frederick Gotham
     
    Frederick Gotham, Oct 16, 2006
    #13
  14. Frederick Gotham wrote:

    >> NULL is never '\0', NULL is 0. If you assign 0 to a char the char gets
    >> the value '\0' by means of habitual conversions, nothing related with
    >> NULL itself.

    >
    > The character literal, '\0' _always_ evaluates to integer zero.


    Try this:
    cout << '\0' << '\n';
    cout << 0 << '\n';

    --
    Salu2
     
    =?ISO-8859-15?Q?Juli=E1n?= Albo, Oct 16, 2006
    #14
  15. Ron Natalie Guest

    Julián Albo wrote:
    > Frederick Gotham wrote:
    >
    >>> NULL is never '\0', NULL is 0. If you assign 0 to a char the char gets
    >>> the value '\0' by means of habitual conversions, nothing related with
    >>> NULL itself.

    >> The character literal, '\0' _always_ evaluates to integer zero.

    >
    > Try this:
    > cout << '\0' << '\n';
    > cout << 0 << '\n';
    >

    Yeah so what? '\0' is an integer zero.
    ostream::eek:perator<< however distinguishes some
    integer types (e.g., char) from others (like int).
    The same is true for char* versus void*.
     
    Ron Natalie, Oct 16, 2006
    #15
  16. =?ISO-8859-15?Q?Juli=E1n?= Albo posted:

    > Try this:
    > cout << '\0' << '\n';



    Also try it's equivalent:

    cout << (char)0 << '\n';

    Indeed, std::eek:stream might define certain overloads for operator<<, but that
    doesn't make "char" any less of an integer type, nor its zero value any less
    of an integer zero value.

    --

    Frederick Gotham
     
    Frederick Gotham, Oct 16, 2006
    #16
  17. Ron Natalie wrote:

    >>>> NULL is never '\0', NULL is 0. If you assign 0 to a char the char gets
    >>>> the value '\0' by means of habitual conversions, nothing related with
    >>>> NULL itself.
    >>> The character literal, '\0' _always_ evaluates to integer zero.

    >>
    >> Try this:
    >> cout << '\0' << '\n';
    >> cout << 0 << '\n';
    >>

    > Yeah so what? '\0' is an integer zero.


    Is an integer type constant, but does not evaluate to nothing but char 0 in
    this case.

    My first paragraph was confuse, certainly. The point is that even if NULL is
    defined as '\0' is not intended to be used like that char literal, but in
    contexts where the concrete integer type does not matter. And if is not
    defined that way, the char also gets the value '\0', so the definition of
    NULL in the implementation is not relevant.

    --
    Salu2
     
    =?ISO-8859-15?Q?Juli=E1n?= Albo, Oct 16, 2006
    #17
  18. =?ISO-8859-15?Q?Juli=E1n?= Albo posted:

    >> Yeah so what? '\0' is an integer zero.

    >
    > Is an integer type constant, but does not evaluate to nothing but char 0
    > in this case.



    Yes. The following two expressions are exactly equivalent:

    (1) '\0'

    (2) (char)0


    "char" is a fully-fledged integer type in every way.

    --

    Frederick Gotham
     
    Frederick Gotham, Oct 16, 2006
    #18
  19. Ron Natalie Guest

    Julián Albo wrote:
    > Ron Natalie wrote:
    >
    >>>>> NULL is never '\0', NULL is 0. If you assign 0 to a char the char gets
    >>>>> the value '\0' by means of habitual conversions, nothing related with
    >>>>> NULL itself.
    >>>> The character literal, '\0' _always_ evaluates to integer zero.
    >>> Try this:
    >>> cout << '\0' << '\n';
    >>> cout << 0 << '\n';
    >>>

    >> Yeah so what? '\0' is an integer zero.

    >
    > Is an integer type constant, but does not evaluate to nothing but char 0 in
    > this case.


    char is an integer type.
    >
    > My first paragraph was confuse, certainly. The point is that even if NULL is
    > defined as '\0' is not intended to be used like that char literal,


    So, that wasn't the question. The question was "can NULL be '\0'.
    I was refuting the argumnt that quoted above that "NULL is never '\0'"
    and that is wrong. The language does not prohibit it.

    There's nothing more or less correct about it being '\0' than 0
    or 0L or false.
     
    Ron Natalie, Oct 16, 2006
    #19
  20. Frederick Gotham wrote:

    >>> Yeah so what? '\0' is an integer zero.

    >>
    >> Is an integer type constant, but does not evaluate to nothing but char 0
    >> in this case.

    >
    > Yes. The following two expressions are exactly equivalent:
    >
    > (1) '\0'
    >
    > (2) (char)0


    Yes, both are char constants, and are used like that were appropriate.

    --
    Salu2
     
    =?ISO-8859-15?Q?Juli=E1n?= Albo, Oct 16, 2006
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    27,267
    Mike Schilling
    Mar 29, 2006
  2. Ian Collins

    assigning pointer to NULL

    Ian Collins, Jan 30, 2008, in forum: C Programming
    Replies:
    27
    Views:
    2,575
    David Thompson
    Feb 10, 2008
  3. aneuryzma
    Replies:
    3
    Views:
    763
    Jim Langston
    Jun 16, 2008
  4. Christopher
    Replies:
    4
    Views:
    459
    Ruben Safir
    Jul 9, 2011
  5. weston
    Replies:
    1
    Views:
    274
    Richard Cornford
    Sep 22, 2006
Loading...

Share This Page