Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   is (!ptr) or (ptr) valid way to check for NULL or NOT NULL? (http://www.velocityreviews.com/forums/t437083-is-ptr-or-ptr-valid-way-to-check-for-null-or-not-null.html)

G Fernandes 02-25-2005 05:26 AM

is (!ptr) or (ptr) valid way to check for NULL or NOT NULL?
 
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.


xarax 02-25-2005 05:30 AM

Re: is (!ptr) or (ptr) valid way to check for NULL or NOT NULL?
 
"G Fernandes" <ge.fernandes@gmail.com> wrote in message
news:1109309175.642496.194930@o13g2000cwo.googlegr oups.com...
> 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.



Ben Pfaff 02-25-2005 05:57 AM

Re: is (!ptr) or (ptr) valid way to check for NULL or NOT NULL?
 
"G Fernandes" <ge.fernandes@gmail.com> writes:

> 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.
--
Ben Pfaff
email: blp@cs.stanford.edu
web: http://benpfaff.org

G Fernandes 02-25-2005 06:24 AM

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

Ben Pfaff wrote:
> "G Fernandes" <ge.fernandes@gmail.com> writes:
>
> > 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.



Thank you Ben and xarax. I appreciate the help.


Joe Smith 02-25-2005 06:32 AM

Re: is (!ptr) or (ptr) valid way to check for NULL or NOT NULL?
 
G Fernandes wrote:
> 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

--
Joe Smith

DHOLLINGSWORTH2 02-26-2005 05:24 AM

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

"G Fernandes" <ge.fernandes@gmail.com> wrote in message
news:1109309175.642496.194930@o13g2000cwo.googlegr oups.com...
> 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.



Keith Thompson 02-26-2005 09:09 AM

Re: is (!ptr) or (ptr) valid way to check for NULL or NOT NULL?
 
"DHOLLINGSWORTH2" <DHOLLINGSWORTH2@cox.net> writes:
> "G Fernandes" <ge.fernandes@gmail.com> wrote in message
> news:1109309175.642496.194930@o13g2000cwo.googlegr oups.com...
>> 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.


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) kst-u@mib.org <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.

Joe Wright 02-26-2005 03:34 PM

Re: is (!ptr) or (ptr) valid way to check for NULL or NOT NULL?
 
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. :-)

--
Joe Wright mailto:joewwright@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---

Keith Thompson 02-26-2005 08:19 PM

Re: is (!ptr) or (ptr) valid way to check for NULL or NOT NULL?
 
Joe Wright <joewwright@comcast.net> writes:
> 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.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <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.

DHOLLINGSWORTH2 02-27-2005 03:07 AM

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

"Keith Thompson" <kst-u@mib.org> wrote in message
news:lnk6ovy9eo.fsf@nuthaus.mib.org...
> "DHOLLINGSWORTH2" <DHOLLINGSWORTH2@cox.net> writes:
>> "G Fernandes" <ge.fernandes@gmail.com> wrote in message
>> news:1109309175.642496.194930@o13g2000cwo.googlegr oups.com...
>>> 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.

>
> 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) kst-u@mib.org
> <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!




All times are GMT. The time now is 07:41 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.