Double conditional on a pointer

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2008
Posts: 628
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

Double conditional on a pointer

 
0
  #1
20 Days Ago
If I have the following setup:

  1.  
  2. Point* MyPoint = Object->GetMyPoint();
  3.  
  4. if(MyPoint->GetValue() != 2)
  5. do something;
  6. else
  7. do something else;

If MyPoint is NULL or invalid, the MyPoint->GetValue() will cause a segfault.

This should fix it:
  1.  
  2. Point* MyPoint = Object->GetMyPoint();
  3. if(MyPoint)
  4. {
  5. if(MyPoint->GetValue() != 2)
  6. do A;
  7. else
  8. do B;
  9. }
  10. else
  11. do B;

But that is quite awkward, as it makes me repeat B and it adds an extra nested layer.

Is there a better way to do this?

Thanks,

Dave
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 314
Reputation: JasonHippy is a jewel in the rough JasonHippy is a jewel in the rough JasonHippy is a jewel in the rough JasonHippy is a jewel in the rough 
Solved Threads: 56
JasonHippy's Avatar
JasonHippy JasonHippy is offline Offline
Posting Whiz
 
0
  #2
20 Days Ago
Originally Posted by daviddoria View Post
If I have the following setup:

  1.  
  2. Point* MyPoint = Object->GetMyPoint();
  3.  
  4. if(MyPoint->GetValue() != 2)
  5. do something;
  6. else
  7. do something else;

If MyPoint is NULL or invalid, the MyPoint->GetValue() will cause a segfault.

This should fix it:
  1.  
  2. Point* MyPoint = Object->GetMyPoint();
  3. if(MyPoint)
  4. {
  5. if(MyPoint->GetValue() != 2)
  6. do A;
  7. else
  8. do B;
  9. }
  10. else
  11. do B;

But that is quite awkward, as it makes me repeat B and it adds an extra nested layer.

Is there a better way to do this?

Thanks,

Dave
This would be the simplest way of doing it:
  1. Point* MyPoint = Object->GetMyPoint();
  2. if(MyPoint && MyPoint->GetValue() != 2)
  3. do A;
  4. else
  5. do B;

Cheers for now,
Jas.
There are 10 types of people in this world.....
Those who understand binary .....
And those who don't!
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 628
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster
 
0
  #3
20 Days Ago
Jas, but won't it still crash evaluating the second part of the conditional?

Dave
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 314
Reputation: JasonHippy is a jewel in the rough JasonHippy is a jewel in the rough JasonHippy is a jewel in the rough JasonHippy is a jewel in the rough 
Solved Threads: 56
JasonHippy's Avatar
JasonHippy JasonHippy is offline Offline
Posting Whiz
 
0
  #4
20 Days Ago
If memory serves, if the left hand condition fails it won't bother evaluating the right hand one.
(depending on how the AND is evaluated...I think it gets evaluated left to right, so I'm pretty certain that if the first condition fails, it will ignore the 2nd condition and just return false! However if it is evaluated right to left, then you could simply swap the conditions over!)

Alternatively if your code is in a function you could do this:
  1. Point* MyPoint = Object->GetMyPoint();
  2.  
  3. // if pointer is not valid, return...
  4. if(!MyPoint)
  5. return with an error condition/value;
  6.  
  7. // if we get this far, the pointer is valid
  8. if(MyPoint->GetValue() != 2)
  9. call function A;
  10. else
  11. call function B;
  12.  
  13. return success condition/value;

Cheers for now,
Jas.
Last edited by JasonHippy; 20 Days Ago at 10:22 am. Reason: smelling pistakes and tpyos
There are 10 types of people in this world.....
Those who understand binary .....
And those who don't!
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 314
Reputation: JasonHippy is a jewel in the rough JasonHippy is a jewel in the rough JasonHippy is a jewel in the rough JasonHippy is a jewel in the rough 
Solved Threads: 56
JasonHippy's Avatar
JasonHippy JasonHippy is offline Offline
Posting Whiz
 
0
  #5
20 Days Ago
Yup, as suspected; logical AND is evaluated left to right, so if the 1st condition fails / is false, then the 2nd condition is ignored and the logical AND operation returns false. The 2nd condition is only evaluated if the 1st condition is true.

So my original post should be fine!

Cheers for now,
Jas.
Last edited by JasonHippy; 20 Days Ago at 10:31 am.
There are 10 types of people in this world.....
Those who understand binary .....
And those who don't!
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC