473,400 Members | 2,145 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,400 software developers and data experts.

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 ->
--
Nov 13 '05 #1
19 2505
Mark A. Odell <no****@embeddedfw.com> 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 (pa*****@cc.helsinki.fi) ---------------------------\
| 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
Nov 13 '05 #2

"Mark A. Odell" <no****@embeddedfw.com> wrote in message
news:Xn********************************@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
Nov 13 '05 #3
"Mike Wahler" <mk******@mkwahler.net> wrote in
news:vb*****************@newsread3.news.pas.earthl 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 ->
--
Nov 13 '05 #4
"Mark A. Odell" <no****@embeddedfw.com> wrote in message
news:Xn********************************@130.133.1. 4...
"Mike Wahler" <mk******@mkwahler.net> wrote in
news:vb*****************@newsread3.news.pas.earthl 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
Nov 13 '05 #5
On 25 Sep 2003 17:59:15 GMT, "Mark A. Odell" <no****@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);
}


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.

Nov 13 '05 #6

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
Nov 13 '05 #7

"Nick Austin" <ni**********@nildram.co.uk> wrote in message
news:m7********************************@4ax.com...
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
Nov 13 '05 #8
"Mike Wahler" <mk******@mkwahler.net> writes:
"Nick Austin" <ni**********@nildram.co.uk> wrote in message
news:m7********************************@4ax.com...
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
Nov 13 '05 #9
"Mark A. Odell" <no****@embeddedfw.com> wrote in message news:<Xn********************************@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
Nov 13 '05 #10
Arthur J. O'Dwyer wrote:
The only implicit conversions are from pointers
to other pointer types, or from arrays to pointers, or between
different arithmetic types.


Conversions from pointers to other pointer types require casts,
unless the conversion is to or from type pointer to void.

--
pete
Nov 13 '05 #11
dis
"Mark A. Odell" <no****@embeddedfw.com> wrote in message
news:Xn********************************@130.133.1. 4...
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) ...


Both code fragments above are compliant. Note however that the argument to
the assert macro must be of type int under the C90 standard (and must be of
scalar type under the C99 standard):

#include <assert.h>
int main(void)
{
char *p = 0;
assert(p != 0); /* compliant under C90 and C99 */
assert(p); /* compliant under C99 only */
return 0;
}

Nov 13 '05 #12
"dis" <di*@hotmail.com> wrote in
news:bl**********@news2.tilbu1.nb.home.nl:
Note however that the argument to
the assert macro must be of type int under the C90 standard (and must be
of scalar type under the C99 standard):

#include <assert.h>
int main(void)
{
char *p = 0;
assert(p != 0); /* compliant under C90 and C99 */
assert(p); /* compliant under C99 only */
return 0;
}


This is a good point to make. Thank you. I hope that C99 compliant
compilers will soon dominate the C compiler market. However, Microsoft
seems not to be too interested judging by some previous posts.

--
- Mark ->
--
Nov 13 '05 #13
In <Xn********************************@130.133.1.4> "Mark A. Odell" <no****@embeddedfw.com> writes:
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) ...


Ever considered reading the FAQ?

5.3: Is the abbreviated pointer comparison "if(p)" to test for non-
null pointers valid?

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #14
Da*****@cern.ch (Dan Pop) wrote in news:bl**********@sunnews.cern.ch:

[snip]
if (pFoo != NULL) ...


Ever considered reading the FAQ?


Yes, I've done so on many occasion. I seem unable to retain the entire
body of knowledge it contains yet still believe to remember it in toto.
You're a bit late to the party on this one, you need to check news more
often.

--
- Mark ->
--
Nov 13 '05 #15
On Fri, 26 Sep 2003 00:55:27 GMT, pete <pf*****@mindspring.com> wrote:
Arthur J. O'Dwyer wrote:
The only implicit conversions are from pointers
to other pointer types, or from arrays to pointers, or between
different arithmetic types.

And from function (designator) to pointer, and non-array lvalue to
(r)value, although the latter especially rather stretches the meaning
of the word. And in C99 pointer to _Bool, just for the heck of it.
Conversions from pointers to other pointer types require casts,
unless the conversion is to or from type pointer to void.


Or to add qualification.

- David.Thompson1 at worldnet.att.net
Nov 13 '05 #16
Dave Thompson wrote:

On Fri, 26 Sep 2003 00:55:27 GMT, pete <pf*****@mindspring.com> wrote:
Arthur J. O'Dwyer wrote:
The only implicit conversions are from pointers
to other pointer types, or from arrays to pointers, or between
different arithmetic types.

And from function (designator) to pointer, and non-array lvalue to
(r)value, although the latter especially rather stretches the meaning
of the word. And in C99 pointer to _Bool, just for the heck of it.
Conversions from pointers to other pointer types require casts,
unless the conversion is to or from type pointer to void.


Or to add qualification.


Thank you.

--
pete
Nov 13 '05 #17
In <Xn********************************@130.133.1.4> "Mark A. Odell" <no****@embeddedfw.com> writes:
Da*****@cern.ch (Dan Pop) wrote in news:bl**********@sunnews.cern.ch:

[snip]
if (pFoo != NULL) ...
Ever considered reading the FAQ?


Yes, I've done so on many occasion. I seem unable to retain the entire
body of knowledge it contains yet still believe to remember it in toto.


Does this somehow mean that Mark A. Odell is exempt from the usual
requirement of checking the FAQ before asking questions?
You're a bit late to the party on this one, you need to check news more
often.


You're in dire need of a clue about how Usenet works!

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #18
Da*****@cern.ch (Dan Pop) wrote in news:bl**********@sunnews.cern.ch:
[snip]
if (pFoo != NULL) ...

Ever considered reading the FAQ?


Yes, I've done so on many occasion. I seem unable to retain the entire
body of knowledge it contains yet still believe to remember it in toto.


Does this somehow mean that Mark A. Odell is exempt from the usual
requirement of checking the FAQ before asking questions?


No, of course not. But I'm sure you know that I have read the FAQ despite
your implication, "Ever" to mean I have never read the FAQ.
You're a bit late to the party on this one, you need to check news more
often.


You're in dire need of a clue about how Usenet works!


So you're telling me that your feed, typically quite responsive wasn't as
of late? I can except that Dan and yes I do know that usenet is not
immediate and has no such guarantee and that newservers may or may not
propagate messages in a time-regular manner.

--
- Mark ->
--
Nov 13 '05 #19
In <Xn********************************@130.133.1.4> "Mark A. Odell" <no****@embeddedfw.com> writes:
Da*****@cern.ch (Dan Pop) wrote in news:bl**********@sunnews.cern.ch:
[snip]

>if (pFoo != NULL) ...

Ever considered reading the FAQ?

Yes, I've done so on many occasion. I seem unable to retain the entire
body of knowledge it contains yet still believe to remember it in toto.


Does this somehow mean that Mark A. Odell is exempt from the usual
requirement of checking the FAQ before asking questions?


No, of course not. But I'm sure you know that I have read the FAQ despite
your implication, "Ever" to mean I have never read the FAQ.


OK, so you're sarcasm impaired. Unfortunately, there is no universally
accepted emoticon for sarcasm.
You're a bit late to the party on this one, you need to check news more
often.


You're in dire need of a clue about how Usenet works!


So you're telling me that your feed, typically quite responsive wasn't as
of late?


I'm telling you that it is downright foolish to make *any* assumptions
about someone's news feed. There are far too many things that can
happen at any type, regardless of its usual quality.

According to the c.l.c image provided my news server, no one else
had pointed you to the FAQ at the time I did it. Not being omniscient,
I had no idea about the contents of the posts that were still traveling
across the wire.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: William Payne | last post by:
Consider the following code: struct foo { char* bar; }; // fooptr is of type foo* if(fooptr != NULL && fooptr->bar != NULL) {
13
by: Don Vaillancourt | last post by:
What's going on with Javascript. At the beginning there was the "undefined" value which represented an object which really didn't exist then came the null keyword. But yesterday I stumbled...
6
by: TJS | last post by:
how to check for null attribute in form input field using IE this does not work in IE var curr_id = tablecells.getAttribute('name'); if (curr_id != null){ .... }
10
by: DevarajA | last post by:
I've read that 0 is the 'null pointer constant', and assigning it to a pointer makes it a 'null pointer'. Is that true? And is the opposite true? (assigning a null pointer to an int sets the int to...
102
by: junky_fellow | last post by:
Can 0x0 be a valid virtual address in the address space of an application ? If it is valid, then the location pointed by a NULL pointer is also valid and application should not receive "SIGSEGV"...
13
by: Federico Balbi | last post by:
Hi, I was wondering if PGSQL has a function similar to binary_checksum() of MS SQL Server 2000. It is pretty handy when it comes to compare rows of data instead of having to write long boolean...
5
by: corepaul | last post by:
I am using Acess 2000 and Windows 2000. I have a form with a text box txLName. tStrGlobal As String is dimensioned globally. The first time I enter the text box, the Enter event with the code ...
9
by: arundelo | last post by:
Is there a way to tell whether accessing a variable will result in an "Undefined variable" E_NOTICE? isset() almost does this, but it also returns false if a variable is set to null: ...
17
by: copx | last post by:
I don't know what to think of the following.. (from the dietlibc FAQ) Q: I see lots of uninitialized variables, like "static int foo;". What gives? A: "static" global variables are initialized...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.