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.
It's not possible to test whether a pointer points to a valid object or not. If the pointer is not null but does not point to a valid object, then using the pointer causes undefined behaviour. To avoid this sort of error, the onus is on you to be careful with the lifetime of objects being pointed to; and the smart pointer classes help with this task.
If
meh
is a raw pointer then there is no difference whatsoever betweenif (meh)
andif (meh != 0)
andif (meh != nullptr)
. They all proceed iff the pointer is not null.There is an implicit conversion from the literal
0
tonullptr
.In C, anything that's not 0 is true. So, you certainly can use:
to safely dereference pointers.
C++11 changes the game a bit,
nullptr_t
is a type of whichnullptr
is an instance; the representation ofnullptr_t
is implementation specific. So a compiler may definenullptr_t
however it wants. It need only make sure it can enforce proper restriction on the casting of anullptr_t
to different types--of which boolean is allowed--and make sure it can distinguish between anullptr_t
and 0.So
nullptr
will be properly and implicitly cast to the booleanfalse
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.
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.