View Single Post

   
  #5 (permalink)  
Old 01-17-2008, 07:14 AM
John Winters
 
Posts: n/a
Default Re: NULL: Is it guaranteed to evaluate 'not true'

In article <Xns94018E057A0BDCopyrightMarkOdell@130.133.1.4> ,
Mark A. Odell <nospam@embeddedfw.com> wrote:
>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) ...


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-documenting.

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.
Reply With Quote