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

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

Post by G Fernande » Sat, 26 Feb 2005 14:26:15


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.
 
 
 

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

Post by xara » Sat, 26 Feb 2005 14:30:53


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.

 
 
 

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

Post by G Fernande » Sat, 26 Feb 2005 15:24:29


to
?:)


Thank you Ben and xarax. I appreciate the help.
 
 
 

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

Post by Joe Smit » Sat, 26 Feb 2005 15:32:21


Yes. See FAQ 5.3 at:

http://www.yqcomputer.com/ ~scs/C-faq/top.html

--
Joe Smith
 
 
 

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

Post by DHOLLINGSW » Sun, 27 Feb 2005 14:24:27


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.
 
 
 

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

Post by Keith Thom » Sun, 27 Feb 2005 18:09:51

"DHOLLINGSWORTH2" < XXXX@XXXXX.COM > writes:



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.yqcomputer.com/ ~scs/C-faq/top.html>
discusses null pointers; it's highly recommended.

--
Keith Thompson (The_Other_Keith) XXXX@XXXXX.COM < http://www.yqcomputer.com/ ~kst>
San Diego Supercomputer Center <*> < http://www.yqcomputer.com/ ~kst>
We must do something. This is something. Therefore, we must do this.
 
 
 

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

Post by Joe Wrigh » Mon, 28 Feb 2005 00:34:59


[ snippage ]


"if (!ptr)" is equivalent to "if (ptr == 0)". But you knew that. :-)

--
Joe Wright mailto: XXXX@XXXXX.COM
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
 
 
 

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

Post by Keith Thom » Mon, 28 Feb 2005 05:19:40

Joe Wright < XXXX@XXXXX.COM > writes:

[...]

Yes. Looks like I picked the wrong week to stop sniffing glue.

--
Keith Thompson (The_Other_Keith) XXXX@XXXXX.COM < http://www.yqcomputer.com/ ~kst>
San Diego Supercomputer Center <*> < http://www.yqcomputer.com/ ~kst>
We must do something. This is something. Therefore, we must do this.
 
 
 

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

Post by DHOLLINGSW » Mon, 28 Feb 2005 12:07:42


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!