473,387 Members | 1,535 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,387 software developers and data experts.

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.

Nov 14 '05 #1
9 1678
"G Fernandes" <ge**********@gmail.com> wrote in message
news:11**********************@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.
Nov 14 '05 #2
"G Fernandes" <ge**********@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: bl*@cs.stanford.edu
web: http://benpfaff.org
Nov 14 '05 #3

Ben Pfaff wrote:
"G Fernandes" <ge**********@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.

Nov 14 '05 #4
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
Nov 14 '05 #5

"G Fernandes" <ge**********@gmail.com> wrote in message
news:11**********************@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.
Nov 14 '05 #6
"DHOLLINGSWORTH2" <DH*************@cox.net> writes:
"G Fernandes" <ge**********@gmail.com> wrote in message
news:11**********************@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) ks***@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.
Nov 14 '05 #7
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:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #8
Joe Wright <jo********@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) ks***@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.
Nov 14 '05 #9

"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
"DHOLLINGSWORTH2" <DH*************@cox.net> writes:
"G Fernandes" <ge**********@gmail.com> wrote in message
news:11**********************@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) ks***@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!
Nov 14 '05 #10

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

Similar topics

1
by: Luis | last post by:
Is there an easier way to check if any of the fields returned in a select statement have null values? After running this command: set rs = conn.execute("select A,B,C,D,E,F,G,H,I,J from...
2
by: sam | last post by:
For Example, If I delete two times like the below code, the program will behave differently. is this correct? As C++ FAQ the delete checks where or not f1 has memory. How can the following program...
2
by: ad | last post by:
I have a string like string sNo. I want to cast it to int with int.Parse(sNo) but if the string is null or empty, it will thow exception. I must check if sNo is not null and is not empty before...
1
by: MattH | last post by:
I can't seem to figure out the syntax for checking for null values in asp.net. Can anyone point me in the right direction? Also, is there a common area on the MSDN for syntax lookup? Thanks!
1
by: WebBuilder451 | last post by:
Why will this not work? it comes back with an error saying it can't work because the value is null this is a dataset value retreaved from db and it is null. call to check for null...
15
by: Tarun Mistry | last post by:
Hi guys, what is the best/correct way to check for a NULL object? I.e. myClass test; if(test == null) {}
4
by: Richard Coltrane | last post by:
Hi there, Im stepping into C# from VB.net. In all the examples ive seen about raising events the following construct is used: if (myevent != null) myevent(this,args); Whats the purpose of...
1
by: Sparky74 | last post by:
Hi Everybody. I have been searching for many hours for an answer to this problem. I hope somebody can help me. I have a C# .NET client application that connects to a TCP/IP C++ server application...
2
by: qwedster | last post by:
Folk! How to programattically check if null value exists in database table (using stored procedure)? I know it's possble in the Query Analyzer (see last SQL query batch statements)? But how...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...

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.