pointer question

I've been trying to learn c++ lately and its becoming a habit of me to validate pointers using these codes.

1
2
3
4
5
  object* pobject = getobject( objectid );
  if ( pobject && pobject->isready() )
{
// do something
}


usually it should be written like this
1
2
3
4
5
6
7
8
  object* pobject = getobject( objectid );
  if ( pobject )
{
     if ( pobject->isready()
     {
       // do something
      }
}


but using the first code always works and i haven't had any crashes yet.

should i drop this habit or is it perfectly fine?
Last edited on
What do you mean by "verify pointers"?
pointer validity, im not sure if its the right way of calling it, im just a beginner
Well I don't really see what you mean by validity.
But I personally never had any problem with the use of pointers, and they are used a lot in C++.

I think it's safe to assume a correct use of pointers if:
*Your program works as intended
*You don't get compiling errors

As long as you understand how pointers work, or if you don't understand it fully, the compiler and the errors you'll get usually fill in the blank and let you know what's wrong.

I hope this answers your question and that it helps you in any way.

Regards,

Hugo.
Yes, that's fine.

What's happening is called "short-circuiting"; in A && B then B will only be evaluated if A is true. This is an optimization enforced by the language: if we knew that A was false, then we wouldn't need to evaluate B to know that the result of the entire expression was false. A similar thing happens with the || operator.

Your if (p && p->ready()) code is a common example of that at work, and is a recognisable idiom. You first evaluate the pointer, and only if the pointer is valid does the pointer get dereferenced. Make sure not to swap the operands, though; p->ready() && p will crash if p == nullptr.

(Caveat you shouldn't need to care about: short-circuiting doesn't occur with overloaded && and || operators, so if in p && p->foo() the result of p->foo() was a user defined type with an overloaded operator&&, then your program will crash if p == nullptr. Of course, (almost) nobody overloads these operators for exactly this reason, so you should be free to safely ignore this paragraph.)
Last edited on
im sorry if my question is not clear, i edited the the question with comparison
thanks for informative answer @TwilightSpectre
consider being able to disable some of the checks in release builds if you trust the code is correct and the pointers can't be null, esp if you are doing this kind of thing inside loops over lots and lots of pointers (hopefully, you don't use too many pointers in general?). Too much error checking can ruin performance. Assert is automatically removed in release builds, so that is one way to handle this. What you are doing is great, in general, just keep in the back of your mind a sanity check on whether you might be over-doing the 'should never happen but check it anyway' stuff. Also, if you don't like the idea of disable the check and the checks are inside a tight inner loop, maybe you should not be using pointers for whatever it is, and look at rewriting to use another data structure or approach.

Topic archived. No new replies allowed.