Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
This seems to work, is it dangerous?

if(pointer && *pointer == value)
{
...
}

Thanks

:Ron
Posted

It is good practice, but not enough.
The problem is that you have no (standard) means to test if that pointer variable contains valid pointer or garbage. The language standard doesn't specify that the variables need to be cleared on creation or not. It is up to the compiler and runtime implementation if int* p will be null by default or not. As I know there is no commonly used compiler that adds the code to zero the variable when declared, because it takes time. And doing the coder's job is not the C/C++ way of thinking. Global variables are zeroed most of the time, but fields and local ones not.
So, to be sure not to run into trouble, make sure to initialize pointers as soon as possible (relatieve to the declaration) - in the same statement, if possible.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 11-Jan-15 14:16pm    
Those are good points, but they are not directly related to the OP's concern. My 5 anyway.
—SA
Zoltán Zörgő 11-Jan-15 14:21pm    
Thank you.
My idea was to highligh that the test OP does won't stop him from gioing crazy because of uninitialized pointers, as garbage will pass the null-test, and might happen that the second expression won't cause access violation either, so everything looks fine... but... :)

Sill, it is not more dangerous than the rest of the code :)
Sergey Alexandrovich Kryukov 11-Jan-15 14:26pm    
Sure, I understand your points and the value of the advice.
In my answer (please see) I pointed out some more...
—SA
You know, programming is a pretty dangerous thing. No, it's not more dangerous. :-)

Please see: http://en.wikipedia.org/wiki/Short-circuit_evaluation[^].

The major thing you should be careful with would be operator precedence. Please see: http://en.cppreference.com/w/cpp/language/operator_precedence[^].

As you can see, '&&' goes at position 13, after comparison operator, so everything's fine. When in doubt, you can use '()'. :-)

—SA
 
Share this answer
 
v3
Comments
Ron Anders 11-Jan-15 15:41pm    
Sorry I did not clarify that upon program start I NULL that pointer. It is then initialized by a DLL I've loaded into my process with the address of a variable in the dll of interest. Given that bad things could happen like the dll not loading, I was going to run the risk of using a null pointer. It looked good to my eyes to do if(pointer && *pointer = value) but cool looking and worrisome are two different things.

I guess I'm just concerned about relying on the short circuiting.

Thank everyone.

:Ron
Sergey Alexandrovich Kryukov 11-Jan-15 15:47pm    
I understand. This is what I assumed in first place. I maintain that you should enjoy your short-circuited expressions under "if". There are safe and quite useful, in several aspects.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900