is (!ptr) or (ptr) valid way to check for NULL or NOT NULL?

Discussion in 'C Programming' started by G Fernandes, Feb 25, 2005.

  1. G Fernandes

    G Fernandes Guest

    Hello fellow C-goers,

    Comparisons of pointer variables to 0 are automatically converted to
    comparisons to NULL (which can be represented at the bit level but
    something non-zero).

    But how about using !ptr or ptr in test conditions (of loops, if or ?:)
    ?

    For example:

    char *ptr = malloc(1);
    if(!ptr)
    {
    /*code*/
    }

    It works for me, but I happen to know that my implementation uses zeros
    to represent NULL. Will this be portable to systems that don't have
    zero based NULL?

    Thanking in advance.
    G Fernandes, Feb 25, 2005
    #1
    1. Advertising

  2. G Fernandes

    xarax Guest

    "G Fernandes" <> wrote in message
    news:...
    > Hello fellow C-goers,
    >
    > Comparisons of pointer variables to 0 are automatically converted to
    > comparisons to NULL (which can be represented at the bit level but
    > something non-zero).
    >
    > But how about using !ptr or ptr in test conditions (of loops, if or ?:)
    > ?
    >
    > For example:
    >
    > char *ptr = malloc(1);
    > if(!ptr)
    > {
    > /*code*/
    > }
    >
    > It works for me, but I happen to know that my implementation uses zeros
    > to represent NULL. Will this be portable to systems that don't have
    > zero based NULL?


    Yes, it will work on platforms that use something
    other than all binary zeroes for the internal
    representation of NULL. The conforming compiler
    will automatically convert between the "source code
    zero" and the internal representation as needed.
    xarax, Feb 25, 2005
    #2
    1. Advertising

  3. G Fernandes

    Ben Pfaff Guest

    "G Fernandes" <> writes:

    > Comparisons of pointer variables to 0 are automatically converted to
    > comparisons to NULL (which can be represented at the bit level but
    > something non-zero).
    >
    > But how about using !ptr or ptr in test conditions (of loops, if or ?:)
    > ?


    Yes, that will also work portably.
    --
    Ben Pfaff
    email:
    web: http://benpfaff.org
    Ben Pfaff, Feb 25, 2005
    #3
  4. G Fernandes

    G Fernandes Guest

    Ben Pfaff wrote:
    > "G Fernandes" <> writes:
    >
    > > Comparisons of pointer variables to 0 are automatically converted

    to
    > > comparisons to NULL (which can be represented at the bit level but
    > > something non-zero).
    > >
    > > But how about using !ptr or ptr in test conditions (of loops, if or

    ?:)
    > > ?

    >
    > Yes, that will also work portably.



    Thank you Ben and xarax. I appreciate the help.
    G Fernandes, Feb 25, 2005
    #4
  5. G Fernandes

    Joe Smith Guest

    G Fernandes wrote:
    > Hello fellow C-goers,
    >
    > Comparisons of pointer variables to 0 are automatically converted to
    > comparisons to NULL (which can be represented at the bit level but
    > something non-zero).
    >
    > But how about using !ptr or ptr in test conditions (of loops, if or ?:)
    > ?
    >
    > For example:
    >
    > char *ptr = malloc(1);
    > if(!ptr)
    > {
    > /*code*/
    > }
    >
    > It works for me, but I happen to know that my implementation uses zeros
    > to represent NULL. Will this be portable to systems that don't have
    > zero based NULL?
    >
    > Thanking in advance.
    >


    Yes. See FAQ 5.3 at:

    http://www.eskimo.com/~scs/C-faq/top.html

    --
    Joe Smith
    Joe Smith, Feb 25, 2005
    #5
  6. "G Fernandes" <> wrote in message
    news:...
    > Hello fellow C-goers,
    >
    > Comparisons of pointer variables to 0 are automatically converted to
    > comparisons to NULL (which can be represented at the bit level but
    > something non-zero).
    >
    > But how about using !ptr or ptr in test conditions (of loops, if or ?:)
    > ?
    >
    > For example:
    >
    > char *ptr = malloc(1);
    > if(!ptr)
    > {
    > /*code*/
    > }
    >
    > It works for me, but I happen to know that my implementation uses zeros
    > to represent NULL. Will this be portable to systems that don't have
    > zero based NULL?
    >
    > Thanking in advance.
    >

    For the most part it will work. However, sometimes the case comes up where
    ptr != 0 , and ptr is not a valid pointer. Don't assume that all invalid
    pointers are zero, only that all zeros are null.
    DHOLLINGSWORTH2, Feb 26, 2005
    #6
  7. "DHOLLINGSWORTH2" <> writes:
    > "G Fernandes" <> wrote in message
    > news:...
    >> Comparisons of pointer variables to 0 are automatically converted to
    >> comparisons to NULL (which can be represented at the bit level but
    >> something non-zero).
    >>
    >> But how about using !ptr or ptr in test conditions (of loops, if or ?:)
    >> ?
    >>
    >> For example:
    >>
    >> char *ptr = malloc(1);
    >> if(!ptr)
    >> {
    >> /*code*/
    >> }
    >>
    >> It works for me, but I happen to know that my implementation uses zeros
    >> to represent NULL. Will this be portable to systems that don't have
    >> zero based NULL?
    >>
    >> Thanking in advance.
    >>

    > For the most part it will work. However, sometimes the case comes up where
    > ptr != 0 , and ptr is not a valid pointer. Don't assume that all invalid
    > pointers are zero, only that all zeros are null.


    First of all, "if (!ptr)" and "if (ptr != 0)" are exactly equivalent;
    they both test whether the pointer is non-null.

    It's guaranteed that malloc() returns either a null pointer or a valid
    pointer (unless memory has been corrupted by something that invoked
    undefined behavior, in which case all bets are off anyway).

    You might as well assume that any non-null pointer is valid, because
    there's no way in standard C to determine that a non-null pointer is
    invalid. You just have to make sure that you don't use any invalid
    pointers in the first place.

    Section 5 of the C FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    discusses null pointers; it's highly recommended.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Feb 26, 2005
    #7
  8. G Fernandes

    Joe Wright Guest

    Keith Thompson wrote:
    [ snippage ]

    > First of all, "if (!ptr)" and "if (ptr != 0)" are exactly equivalent;
    > they both test whether the pointer is non-null.
    >
    > It's guaranteed that malloc() returns either a null pointer or a valid
    > pointer (unless memory has been corrupted by something that invoked
    > undefined behavior, in which case all bets are off anyway).
    >
    > You might as well assume that any non-null pointer is valid, because
    > there's no way in standard C to determine that a non-null pointer is
    > invalid. You just have to make sure that you don't use any invalid
    > pointers in the first place.
    >
    > Section 5 of the C FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    > discusses null pointers; it's highly recommended.
    >


    "if (!ptr)" is equivalent to "if (ptr == 0)". But you knew that. :)

    --
    Joe Wright mailto:
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
    Joe Wright, Feb 26, 2005
    #8
  9. Joe Wright <> writes:
    > Keith Thompson wrote:
    > [ snippage ]
    >
    >> First of all, "if (!ptr)" and "if (ptr != 0)" are exactly equivalent;
    >> they both test whether the pointer is non-null.

    [...]
    > "if (!ptr)" is equivalent to "if (ptr == 0)". But you knew that. :)


    Yes. Looks like I picked the wrong week to stop sniffing glue.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Feb 26, 2005
    #9
  10. "Keith Thompson" <> wrote in message
    news:...
    > "DHOLLINGSWORTH2" <> writes:
    >> "G Fernandes" <> wrote in message
    >> news:...
    >>> Comparisons of pointer variables to 0 are automatically converted to
    >>> comparisons to NULL (which can be represented at the bit level but
    >>> something non-zero).
    >>>
    >>> But how about using !ptr or ptr in test conditions (of loops, if or ?:)
    >>> ?
    >>>
    >>> For example:
    >>>
    >>> char *ptr = malloc(1);
    >>> if(!ptr)
    >>> {
    >>> /*code*/
    >>> }
    >>>
    >>> It works for me, but I happen to know that my implementation uses zeros
    >>> to represent NULL. Will this be portable to systems that don't have
    >>> zero based NULL?
    >>>
    >>> Thanking in advance.
    >>>

    >> For the most part it will work. However, sometimes the case comes up
    >> where
    >> ptr != 0 , and ptr is not a valid pointer. Don't assume that all invalid
    >> pointers are zero, only that all zeros are null.

    >
    > First of all, "if (!ptr)" and "if (ptr != 0)" are exactly equivalent;
    > they both test whether the pointer is non-null.
    >
    > It's guaranteed that malloc() returns either a null pointer or a valid
    > pointer (unless memory has been corrupted by something that invoked
    > undefined behavior, in which case all bets are off anyway).
    >
    > You might as well assume that any non-null pointer is valid, because
    > there's no way in standard C to determine that a non-null pointer is
    > invalid. You just have to make sure that you don't use any invalid
    > pointers in the first place.
    >
    > Section 5 of the C FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    > discusses null pointers; it's highly recommended.
    >
    > --
    > Keith Thompson (The_Other_Keith)
    > <http://www.ghoti.net/~kst>
    > San Diego Supercomputer Center <*>
    > <http://users.sdsc.edu/~kst>
    > We must do something. This is something. Therefore, we must do this.


    You seem to have forgotten what was written. The use of malloc for "For the
    Sake of Example" and not the Subject of his question.

    If anything I've said here is incorrect I'll shoot myself!
    DHOLLINGSWORTH2, Feb 27, 2005
    #10
    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. Sid
    Replies:
    5
    Views:
    1,064
  2. Heiko Vogel
    Replies:
    3
    Views:
    547
    Method Man
    Sep 14, 2004
  3. franco ziade

    const ptr to const ptr ?

    franco ziade, Feb 17, 2005, in forum: C Programming
    Replies:
    3
    Views:
    387
    franco ziade
    Feb 17, 2005
  4. Jason

    difference between *ptr++ and ++*ptr ?

    Jason, May 15, 2005, in forum: C Programming
    Replies:
    19
    Views:
    6,512
    Chris Torek
    May 19, 2005
  5. James Harris

    Why not realloc(&ptr, ...) and free(&ptr)?

    James Harris, Aug 5, 2013, in forum: C Programming
    Replies:
    31
    Views:
    490
    Tim Rentsch
    Aug 8, 2013
Loading...

Share This Page