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

G

G Fernandes

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

xarax

G Fernandes said:
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.
 
B

Ben Pfaff

G Fernandes said:
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.
 
J

Joe Smith

G said:
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
 
D

DHOLLINGSWORTH2

G Fernandes said:
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.
 
K

Keith Thompson

DHOLLINGSWORTH2 said:
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.
 
J

Joe Wright

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. :)
 
K

Keith Thompson

Joe Wright said:
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.
 
D

DHOLLINGSWORTH2

Keith Thompson said:
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) (e-mail address removed)
<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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top