Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8675541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:58:29+00:00 2026-06-12T19:58:29+00:00

Say we have two pointers to objects that can contain a property equal and

  • 0

Say we have two pointers to objects that can contain a property equal and we want to check it. But first, we need to check whether the pointers are initialized or not.
What option is usually preferred? will it have some micro impact on performance?

if(p1 && p2 && p1->getA() == p2->getB()){
    execute fancy code
}

or:

if(p1 && p2){
    if(p1->getA() == p2->getB()){
        execute fancy code
    }
}

I was wondering what is usual preferred.

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T19:58:30+00:00Added an answer on June 12, 2026 at 7:58 pm

    First, if (ptr) doesn’t check whether a pointer is intialised or not, it checks if it isn’t NULL. You can initialize a pointer to NULL and the condition wouldn’t hold.

    Second, there are two cases to be treated:

    1) the pointers are allowed by the code logic to be NULL

    In this case, you most certainly want different behavior for the two cases. So what would be appropriate is:

    if ( ptr )
    {  
       ptr->foo();
       //...
    }
    else
    {
       //...
    }
    

    The second syntax doesn’t make much sense semantically if ( ptr && ptr->foo() ) implies that you want to be sure ptr isn’t NULL before calling foo(), instead of grouping the logic bound to the case where foo isn’t false into a use-case.

    2) the pointers aren’t allowed to be NULL

    If they’re not allowed to be NULL, then you should deal with the case that they are NULL, not by excluding it completely, which is what

    if ( ptr && ptr->foo() )
    

    does. But by making it burn:

    if ( !ptr )
        throw std::exception("WTF! This shouldn't be NULL");
    

    ptr && ptr->foo() seems like it’s meant to prevent crashes, but at the same time hide bugs. In a clean logic, you wouldn’t need to check for NULL. If the object wasn’t created and the pointer didn’t point to anything meaningful, you’d have a bug regardless of whether you call foo or not, so you should deal with it, not hide it behind a check.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have two classes and have a requirement that the primary key property
Say I have two containers storing pointers to the same objects: std::list<Foo*> fooList; std::vector<Foo*>
I have an array of objects that i want to sort by two keys.
I have an array that contains pointers. How can I swap two pointers -
Say I have two Java apps that I wrote: Ping.jar and Pong.jar and they
Say I have two functions that expect ...rest parameters private function a(...myParams):void { trace(myParams.length);
I have a class, and the pointers to the objects of this class need
Lets say you have various objects of arbitrary type that you would like to
I have been trying to understand function pointers in C++ so that I can
Let's say I have two forms in a delphi project, I want to be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.