Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > NULL: Is it guaranteed to evaluate 'not true'

Reply
Thread Tools

NULL: Is it guaranteed to evaluate 'not true'

 
 
Mark A. Odell
Guest
Posts: n/a
 
      09-25-2003
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 ->
--
 
Reply With Quote
 
 
 
 
Joona I Palaste
Guest
Posts: n/a
 
      09-25-2003
Mark A. Odell <(E-Mail Removed)> scribbled the following:
> 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);
> }


AFAIK it's indeed guaranteed to evaluate as "not true". Some C expert
will probably offer a more qualified opinion here.

--
/-- Joona Palaste ((E-Mail Removed)) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"This isn't right. This isn't even wrong."
- Wolfgang Pauli
 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      09-25-2003

"Mark A. Odell" <(E-Mail Removed)> wrote in message
news:Xns94018E49B3212CopyrightMarkOdell@130.133.1. 4...
> Is NULL guaranteed to evaluate 'not true',


Yes.

> e.g. is it completely safe and
> portable to write:
>
> char *pFoo = malloc(1024);


Portable, but not safe, since you didn't check
if malloc() succeeded or failed. If it failed,
then a dereference of 'pFoo' will give undefined
behavior.

> if (pFoo)


This will be true if 'pFoo' is not == NULL.

> {
> /* use pFoo
> */
> free(pFoo);
> }
>
> Or must I check against NULL explicitly as in:
>
> if (pFoo != NULL) ...


No, you don't have to, but many coders prefer to
explicitly compare against NULL in the interest
of clarity.

-Mike


 
Reply With Quote
 
Mark A. Odell
Guest
Posts: n/a
 
      09-25-2003
"Mike Wahler" <(E-Mail Removed)> wrote in
news:vbGcb.3403$(E-Mail Removed) ink.net:

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

>
> Yes.
>
>> e.g. is it completely safe and
>> portable to write:
>>
>> char *pFoo = malloc(1024);

>
> Portable, but not safe, since you didn't check
> if malloc() succeeded or failed.


I didn't? What's the following if (pFoo) for then? I did not dereference
pFoo until after the check if (pFoo).

> If it failed,
> then a dereference of 'pFoo' will give undefined
> behavior.
>
>> if (pFoo)

>
> This will be true if 'pFoo' is not == NULL.
>
>> {
>> /* use pFoo
>> */
>> free(pFoo);
>> }
>>
>> Or must I check against NULL explicitly as in:
>>
>> if (pFoo != NULL) ...

>
> No, you don't have to, but many coders prefer to
> explicitly compare against NULL in the interest
> of clarity.


I am certainly not one of them. I prefer C's "boolean" comparisons to
explicit values. Thanks for the reply.

--
- Mark ->
--
 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      09-25-2003
"Mark A. Odell" <(E-Mail Removed)> wrote in message
news:Xns9401911CD38EDCopyrightMarkOdell@130.133.1. 4...
> "Mike Wahler" <(E-Mail Removed)> wrote in
> news:vbGcb.3403$(E-Mail Removed) ink.net:
>
> >> NULL: Is it guaranteed to evaluate 'not true'

> >
> > Yes.
> >
> >> e.g. is it completely safe and
> >> portable to write:
> >>
> >> char *pFoo = malloc(1024);

> >
> > Portable, but not safe, since you didn't check
> > if malloc() succeeded or failed.

>
> I didn't? What's the following if (pFoo) for then? I did not dereference
> pFoo until after the check if (pFoo).


I'm still asleep it seems.

> > If it failed,
> > then a dereference of 'pFoo' will give undefined
> > behavior.
> >
> >> if (pFoo)

> >
> > This will be true if 'pFoo' is not == NULL.
> >
> >> {
> >> /* use pFoo
> >> */
> >> free(pFoo);
> >> }
> >>
> >> Or must I check against NULL explicitly as in:
> >>
> >> if (pFoo != NULL) ...

> >
> > No, you don't have to, but many coders prefer to
> > explicitly compare against NULL in the interest
> > of clarity.

>
> I am certainly not one of them.


Nor am I, except when constrained by coding standards
that require it.

<I prefer C's "boolean" comparisons to
> explicit values.


As do I.

> Thanks for the reply.


Thanks for the wake-up.

-Mike


 
Reply With Quote
 
Nick Austin
Guest
Posts: n/a
 
      09-25-2003
On 25 Sep 2003 17:59:15 GMT, "Mark A. Odell" <(E-Mail Removed)>
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);
>}


Correct and entirely safe.

if() expects an integer expression, which conventionally would be
the result yielded by a relational operator such as !=. This
result will either by zero (false) or non-zero (true).

If if() is supplied with a pointer expression this causes an
implicit pointer-to-integer conversion. In this case a
non-NULL pointer is converted to a non-zero integer. The NULL
pointer is converted to zero.

Nick.

 
Reply With Quote
 
Arthur J. O'Dwyer
Guest
Posts: n/a
 
      09-25-2003

On Thu, 25 Sep 2003, Nick Austin wrote:
>
> On 25 Sep 2003 17:59:15 GMT, "Mark A. Odell" wrote:
> >
> >Is NULL guaranteed to evaluate 'not true', e.g. is it completely safe and
> >portable to write:
> >
> >char *pFoo = malloc(1024);
> >if (pFoo)

>
> Correct and entirely safe.
>
> if() expects an integer expression,


Not true. 'if' expects to be controlled by an expression of
*scalar* type (basically, anything except an aggregate).

> which conventionally would be
> the result yielded by a relational operator such as !=. This
> result will either by zero (false) or non-zero (true).


Sort of. 'if' compares its controlling expression to 0. If
it's not equal to 0, the body of the 'if' is executed. Otherwise,
the body of the 'if' is *not* executed. That's all.

For example,

if (foo)

is exactly equivalent to

if ((foo) != 0)

[you see now why aggregate types are not permitted in 'if'
conditions]. So, specifically,

if (pFoo)
if ((pFoo) != 0)

are equivalent expressions. And since 0 is in a pointer
context here, it's equivalent to a null pointer of type 'char *';
that is,

if (pFoo)
if ((pFoo) != NULL)
if ((pFoo) != (char*)0)

are also all equivalent. And of course they all do what you'd
expect.


> If if() is supplied with a pointer expression this causes an
> implicit pointer-to-integer conversion.


Nonsense. There is no "implicit pointer-to-integer conversion."
Ever. Under any circumstances. The language specifically
forbids it. The only implicit conversions are from pointers
to other pointer types, or from arrays to pointers, or between
different arithmetic types. That's it. (Unless I missed one.)

-Arthur
 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      09-25-2003

"Nick Austin" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> if() expects an integer expression, which conventionally would be
> the result yielded by a relational operator such as !=. This
> result will either by zero (false) or non-zero (true).
>
> If if() is supplied with a pointer expression this causes an
> implicit pointer-to-integer conversion.


This is news to me. Chapter and verse?

-Mike


 
Reply With Quote
 
Micah Cowan
Guest
Posts: n/a
 
      09-25-2003
"Mike Wahler" <(E-Mail Removed)> writes:

> "Nick Austin" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
> > if() expects an integer expression, which conventionally would be
> > the result yielded by a relational operator such as !=. This
> > result will either by zero (false) or non-zero (true).
> >
> > If if() is supplied with a pointer expression this causes an
> > implicit pointer-to-integer conversion.

>
> This is news to me. Chapter and verse?


(To me too...)

Actually, the "implied conversion" would be integer-to-pointer,
since (according to the standard's definition), the scalar
expression will be compared with 0, requiring 0 to be converted
to NULL first.

(Naturally, by the as-if clause, no actual conversion is likely
in practice...)

-Micah
 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      09-25-2003
"Mark A. Odell" <(E-Mail Removed)> wrote in message news:<Xns94018E49B3212CopyrightMarkOdell@130.133.1 .4>...
> Is NULL guaranteed to evaluate 'not true',


Yes. The 'truth' of an arbitrary expressions X is determined by whether
"the expression compares unequal to 0." i.e. (X != 0).

> e.g. is it completely safe and
> portable to write:
>
> char *pFoo = malloc(1024);
> if (pFoo)
> {
> /* use pFoo
> */
> free(pFoo);
> }


Yes.

>
> Or must I check against NULL explicitly as in:
>
> if (pFoo != NULL) ...


No.

--
Peter
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
70-547 Evaluate Proof of Concept =?Utf-8?B?THVsdQ==?= MCSD 1 01-31-2006 03:24 AM
Evaluate Microsoft Skills Assessment Support Information (SASI) si =?Utf-8?B?RGlzbmV0IEFkbWluaXN0cmF0b3I=?= MCSE 1 02-24-2005 04:08 PM
optimize error:left bound range doesn't evaluate to a const. senthil VHDL 0 03-04-2004 03:02 AM
How to evaluate control Yuriy ASP .Net 0 08-12-2003 03:19 PM
Function to evaluate whether a string is all UPPER Brad Smith ASP .Net 2 06-27-2003 04:14 PM



Advertisments