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

  • Home
  • SEARCH
  • 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 8158799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:43:59+00:00 2026-06-06T17:43:59+00:00

In a couple of my older code projects when I had never heard of

  • 0

In a couple of my older code projects when I had never heard of smart pointers, whenever I needed to check whether the pointer still pointed to a valid object, I would always do something like this…

object * meh = new object;
if(meh) 
    meh->member;

Or when I needed to delete the object safely, something like this

if(meh)
{
    delete meh;
    meh = 0;
}

Well, now I have learned about the problems that can arise from using objects and pointers in boolean expressions both with literal numbers, the hard way :. And now I’ve also learned of the not so new but pretty cool feature of C++, the nullptr keyword. But now I’m curious.

I’ve already gone through and revised most of my code so that, for example, when deleting objects I now write

if(meh)
{
    delete meh;
    meh = nullptr;
}

Now I’m wondering about the boolean. When you pass just say an int into an if statement like this,

int meh;
if(meh)

Then it implicitly checks for zero without you needing to write it.

if(meh == 0) // does the exact same check

Now, will C++ do the same for pointers? If pass in a char * like this to an if statement?

char * meh;
if(meh)

Then will it implicitly compare it with nullptr? Because of how long I have been writing these ifs like this, it is second nature at this point to check if the pointers valid before using by typing if (object *) and then calling its members. If this is not the functionality why not? Too difficult to implement? Would solve some problems by removing yet another tiny way you could mess up your code.

  • 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-06T17:44:01+00:00Added an answer on June 6, 2026 at 5:44 pm

    In C, anything that’s not 0 is true. So, you certainly can use:

    if (ptrToObject) 
        ptrToObject->doSomething();
    

    to safely dereference pointers.

    C++11 changes the game a bit, nullptr_t is a type of which nullptr is an instance; the representation of nullptr_t is implementation specific. So a compiler may define nullptr_t however it wants. It need only make sure it can enforce proper restriction on the casting of a nullptr_t to different types–of which boolean is allowed–and make sure it can distinguish between a nullptr_t and 0.

    So nullptr will be properly and implicitly cast to the boolean false so long as the compiler follows the C++11 language specification. And the above snippet still works.

    If you delete a referenced object, nothing changes.

    delete ptrToObject;
    assert(ptrToObject);
    ptrToObject = nullptr;
    assert(!ptrToObject);    
    

    Because of how long I have been writing these ifs like this, it is second nature at this point to check if the pointers valid before using by typing if (object *) and then calling it’s members.

    No. Please maintain a proper graph of objects (preferably using unique/smart pointers). As pointed out, there’s no way to determine if a pointer that is not nullptr points to a valid object or not. The onus is on you to maintain the lifecycle anyway.. this is why the pointer wrappers exist in the first place.

    In fact, because the life-cycle of shared and weak pointers are well defined, they have syntactic sugar that lets you use them the way you want to use bare pointers, where valid pointers have a value and all others are nullptr:

    Shared

    #include <iostream>
    #include <memory>
    
    void report(std::shared_ptr<int> ptr) 
    {
        if (ptr) {
            std::cout << "*ptr=" << *ptr << "\n";
        } else {
            std::cout << "ptr is not a valid pointer.\n";
        }
    }
    
    int main()
    {
        std::shared_ptr<int> ptr;
        report(ptr);
    
        ptr = std::make_shared<int>(7);
        report(ptr);
    }
    

    Weak

    #include <iostream>
    #include <memory>
    
    void observe(std::weak_ptr<int> weak) 
    {
        if (auto observe = weak.lock()) {
            std::cout << "\tobserve() able to lock weak_ptr<>, value=" << *observe << "\n";
        } else {
            std::cout << "\tobserve() unable to lock weak_ptr<>\n";
        }
    }
    
    int main()
    {
        std::weak_ptr<int> weak;
        std::cout << "weak_ptr<> not yet initialized\n";
        observe(weak);
    
        {
            auto shared = std::make_shared<int>(42);
            weak = shared;
            std::cout << "weak_ptr<> initialized with shared_ptr.\n";
            observe(weak);
        }
    
        std::cout << "shared_ptr<> has been destructed due to scope exit.\n";
        observe(weak);
    }
    

    Now, will C++ do the same for pointers? If pass in a char * like this to an if statement?

    So to answer the question: with bare pointers, no. With wrapped pointers, yes.

    Wrap your pointers, folks.

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

Sidebar

Related Questions

We have an older site which still uses the ancient gat Analytics tracking code
With RAD Version: 7.5.3, Java 1.5. I have a couple of different projects. I
I have a couple of classes (for this example anyway) that use code first
In an older version of our code, we called out from Perl to do
So I'm having a couple of troubles with my code. For starters, I have
I am trying a couple of tutorials from http://nehe.gamedev.net , in order to learn
Im using a couple of JSON calls to render data, etc etc. In order
Couple of months ago, we revamped our web site. We adopted totally new site
couple of quick questions 1) In the local search results - we can get
couple days ago I've created spring webapp with scalate template endine. Everything worked great

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.