473,327 Members | 2,012 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,327 software developers and data experts.

can "if (ptr)" cause problems?

A static code analysis tool gave me a warning on

if (ptr && ptr->data) { ... }

I assumed the tool doesn't get the "short circuit behaviour" in
the if statement.

But a collegue said it may be the missing check ot ptr against
NULL.

What happens on an architecture where NULL isn't address
with all bits set to zero (e.g. 0xffff)? In this case my if-statement
will fail.

Does that mean the only portable way is "if (ptr != NULL)?.
if (ptr) and if (!ptr) would then be non-portable?

Thanks for your help.
Nov 17 '06 #1
6 2227
vl106:
A static code analysis tool gave me a warning on

if (ptr && ptr->data) { ... }

What kind of warning?

All I can tell you is that the code is equivalent to:

if (ptr) if (ptr->data) ... ;

--

Frederick Gotham
Nov 17 '06 #2

vl106 wrote:
A static code analysis tool gave me a warning on

if (ptr && ptr->data) { ... }

I assumed the tool doesn't get the "short circuit behaviour" in
the if statement.

But a collegue said it may be the missing check ot ptr against
NULL.

What happens on an architecture where NULL isn't address
with all bits set to zero (e.g. 0xffff)? In this case my if-statement
will fail.

Does that mean the only portable way is "if (ptr != NULL)?.
if (ptr) and if (!ptr) would then be non-portable?

Thanks for your help.
It could be a problem, but not for the reasons stated.

If ptr is uninitialized, the code invokes undefined behavior.

What (exactly) was the warning message?

Nov 17 '06 #3
"vl106" <vl***@hotmail.comwrites:
A static code analysis tool gave me a warning on

if (ptr && ptr->data) { ... }

I assumed the tool doesn't get the "short circuit behaviour" in
the if statement.

But a collegue said it may be the missing check ot ptr against
NULL.
This is a FAQ.

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

A: When C requires the Boolean value of an expression, a false
value is inferred when the expression compares equal to zero,
and a true value otherwise. That is, whenever one writes

if(expr)

where "expr" is any expression at all, the compiler essentially
acts as if it had been written as

if((expr) != 0)

Substituting the trivial pointer expression "p" for "expr", we
have

if(p) is equivalent to if(p != 0)

and this is a comparison context, so the compiler can tell that
the (implicit) 0 is actually a null pointer constant, and use
the correct null pointer value. There is no trickery involved
here; compilers do work this way, and generate identical code
for both constructs. The internal representation of a null
pointer does *not* matter.

The boolean negation operator, !, can be described as follows:

!expr is essentially equivalent to (expr)?0:1
or to ((expr) == 0)

which leads to the conclusion that

if(!p) is equivalent to if(p == 0)

"Abbreviations" such as if(p), though perfectly legal, are
considered by some to be bad style (and by others to be good
style; see question 17.10).

See also question 9.2.

References: K&R2 Sec. A7.4.7 p. 204; ISO Sec. 6.3.3.3,
Sec. 6.3.9, Sec. 6.3.13, Sec. 6.3.14, Sec. 6.3.15, Sec. 6.6.4.1,
Sec. 6.6.5; H&S Sec. 5.3.2 p. 122.

--
Just another C hacker.
Nov 17 '06 #4
dc*****@connx.com wrote:
vl106 wrote:

if (ptr && ptr->data) { ... }
If ptr is uninitialized, the code invokes undefined behavior.
UB also results if the value of ptr has been passed to free().

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Nov 17 '06 #5
>A static code analysis tool gave me a warning on
>
if (ptr && ptr->data) { ... }

I assumed the tool doesn't get the "short circuit behaviour" in
the if statement.
Tools and ANSI-compliant C compilers are different things.
It might be complaining about ptr being *uninitialized*, rather
than a null pointer.
>But a collegue said it may be the missing check ot ptr against
NULL.
This statement is equivalent to
if (ptr != NULL && ptr->data != NULL) { ... }

*EVEN IF* the internal bit representation of a null pointer
is not 0xdeadbeef (or on 64-bit systems, 0xdeadbeefdeadbeef).
>What happens on an architecture where NULL isn't address
with all bits set to zero (e.g. 0xffff)? In this case my if-statement
will fail.
No, it won't fail. Your statement works regardless of the bit
representation of a null pointer. ANSI C makes no guarantee that
a null pointer MUST be represented by 0xdeadbeef, even on 32-bit
systems, and some of them don't.
>Does that mean the only portable way is "if (ptr != NULL)?.
if (ptr) and if (!ptr) would then be non-portable?
No.
Nov 17 '06 #6
Christopher Benson-Manica wrote:
>
dc*****@connx.com wrote:
vl106 wrote:
>
if (ptr && ptr->data) { ... }
If ptr is uninitialized, the code invokes undefined behavior.

UB also results if the value of ptr has been passed to free().
.... not if the value of ptr is equal to NULL.

--
pete
Nov 18 '06 #7

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

Similar topics

10
by: george young | last post by:
I had developed the habit of using the neat python form: if someinstance: someinstance.memb() because it seems cleaner than "if someinstance is not None". {please no flames about "is not None"...
3
by: Y.S. | last post by:
Hello All, I have the following schema: .... <xs:element name="data_type_id" type="xs:unsignedShort" /> <xs:element name="data_value" type="xs:anyType" /> .... What I want to do is to...
1
by: Mark Richards | last post by:
The solutions for the following problems seems to be simple but I did not found a (convenient) solution: Assume we have a number of elements of the same type under a common parent e.g. <person...
40
by: Steve Juranich | last post by:
I know that this topic has the potential for blowing up in my face, but I can't help asking. I've been using Python since 1.5.1, so I'm not what you'd call a "n00b". I dutifully evangelize on the...
145
by: Sidney Cadot | last post by:
Hi all, In a discussion with Tak-Shing Chan the question came up whether the as-if rule can cover I/O functions. Basically, he maintains it can, and I think it doesn't. Consider two...
35
by: David Cleaver | last post by:
Hello all, I was wondering if there were some sort of limitations on the "if" statement? I'm writing a program which needs to check a bunch of conditions all at the same time (basically). And...
2
by: Miguel Dias Moura | last post by:
Hi, i have this code line in a script in my ASP.net / VB web site: dim msgNewsletterAction msgNewsletterAction = Request.Form("newsletterAction") if msgNewsletterAction = "go" Then Code1
10
by: fei.liu | last post by:
Consider the following sample code char * ptr = "hello"; char carray = "hello"; int main(void){ } What does the standard have to say about the storage requirement about ptr and carray? Is...
37
by: jht5945 | last post by:
For example I wrote a function: function Func() { // do something } we can call it like: var obj = new Func(); // call it as a constructor or var result = Func(); // call it as...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.