Professional Web Applications Themes

NULL: Is it guaranteed to evaluate 'not true' - Linux Setup, Configuration & Administration

Is NULL guaranteed to evaluate 'not true', e.g. is it completely safe and portable to write: char *pFoo = malloc(1024); if (pFoo) { /* use pFoo */ free(pFoo); } Or must I check against NULL explicitly as in: if (pFoo != NULL) ... Thanks. -- - Mark -> --...

  1. #1

    Default NULL: Is it guaranteed to evaluate 'not true'

    Is NULL guaranteed to evaluate 'not true', e.g. is it completely safe and
    portable to write:

    char *pFoo = malloc(1024);
    if (pFoo)
    {
    /* use pFoo
    */
    free(pFoo);
    }

    Or must I check against NULL explicitly as in:

    if (pFoo != NULL) ...

    Thanks.

    --
    - Mark ->
    --
    Mark Guest

  2. #2

    Default Re: NULL: Is it guaranteed to evaluate 'not true'

    "Mark A. Odell" <com> wrote in
    news:133.1.4:

    I meant to post this in comp.lang.c. I ended up doing so and I'll answer
    my post here with the answers received there.
     

    Yes.
     

    This is safe.
     

    No, they are equivalent.

    --
    - Mark ->
    --
    Mark Guest

  3. #3

    Default Re: NULL: Is it guaranteed to evaluate 'not true'

    Mark A. Odell wrote:
     

    the "c programming language" (k&r book) can be a nice addition
    to the bookshelf
    ..
    --
    /// Michael J. Tobler: motorcyclist, surfer, skydiver, \\\
    \\\ and author: "Inside Linux", "C++ HowTo", "C++ Unleashed" ///
    \\\ http://pages.sbcglobal.net/mtobler/mjt_linux_page.html ///
    Weiler's Law: Nothing is impossible for the man who doesn't
    have to do it himself.

    mjt Guest

  4. #4

    Default Re: NULL: Is it guaranteed to evaluate 'not true'

    mjt <com> wrote in
    news:TLPcb.519$news.prodigy.com:
     
    >
    > the "c programming language" (k&r book) can be a nice addition
    > to the bookshelf[/ref]

    Yes, it look fine there too. I have both K&R1 and 2. Just not at work.

    --
    - Mark ->
    --
    Mark Guest

  5. #5

    Default Re: NULL: Is it guaranteed to evaluate 'not true'

    In article <133.1.4>,
    Mark A. Odell <com> wrote: 

    Yes, what you want to do is safe, but it's important to differentiate
    between NULL (a pre-processor macro) and a null pointer. I think
    the question which you meant to ask was "Is a null pointer guaranteed
    to evaluate 'not true'?", to which the answer is yes.

    Some (including me) would argue that it's better style to include the
    explicit comparison with NULL, because it makes the code more
    self-doenting.

    HTH
    John
    --
    The Linux Emporium - the source for Linux in the UK
    See http://www.linuxemporium.co.uk/

    We had a woodhenge here once but it rotted.
    John Guest

Similar Threads

  1. Replies: 4
    Last Post: December 13th, 04:31 PM
  2. Replies: 0
    Last Post: November 14th, 07:45 AM
  3. Replies: 0
    Last Post: November 14th, 12:36 AM
  4. Replies: 0
    Last Post: November 13th, 03:58 PM
  5. #26232 [NEW]: in_array produces true when bool true is in haystack
    By me at my dot house in forum PHP Development
    Replies: 0
    Last Post: November 13th, 03:14 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139