-1

I am working on a simple example. Let s say that I have an object Object my_object and I want to check if the object is null.

Therefore, I instantiate the object:

auto my_object = createMyObject(param_object_1);

The idea, is to check whether the object is null or not. If I am not mistaken (I am really new in C++), I have tried

Check if reference is NULL

I believe this is not an option (even if compiling) since references can never be NULL, so I have discarded it.

EXPECT_TRUE(my_object != NULL);

Check if != to nullptr

My next try has been to check if a pointer to the object is null.

auto my_object_ptr = std::make_shared<Object>(my_object);
EXPECT_TRUE(my_object_ptr != nullptr);

This code also compiles but I am not sure if this is the solution I was looking for. My intentions is to check if the pointer is pointing to a null object.

Is it a valid way to do it? If not, what s the best practise to check if the object is empty?

bellotas
  • 2,349
  • 8
  • 29
  • 60
  • 7
    What type does `createMyObject` return? Is it just a `Object`? Objects can't "be null" in C++. Or is it a (smart) pointer? References can also not "be null" in C++, but comparing references to `NULL`, does compare the object referenced to `NULL`, not the reference as an entity itself. – user17732522 Feb 02 '22 at 11:35
  • 2
    `EXPECT_TRUE(my_object_ptr.get() != nullptr);` does the Trick. – πάντα ῥεῖ Feb 02 '22 at 11:36
  • 2
    @πάνταῥεῖ Just `EXPECT_TRUE(my_object_ptr)` probably does the same. – user17732522 Feb 02 '22 at 11:36
  • 4
    Objects are not null (in general), pointers can be null though. – Jarod42 Feb 02 '22 at 11:37
  • 3
    Regarding your last snippet, unless you've done something insanely odd like specialize `std::make_shared` for your object type, `EXPECT_TRUE(my_object_ptr != nullptr);` will *never* trip false. Either `std::make_shared` will *succeed*, or it will throw an exception (which you're not catching) well-past your test. – WhozCraig Feb 02 '22 at 11:40
  • 1
    Objects themselves are not null. Never. Even pointer as an object is not null, it only may represent null (have nullptr value). I think you need to specify what you want to achieve/what problem you want to solve. I've checked your profile and I guess you want to just copy some solution from java/other language which may be very inconvenient ;) – Karol T. Feb 02 '22 at 11:41
  • Was trying to simplify a test. But thanks for confirming that objects can not be null. I guess the way to go then is something like EXPECT_EQ(my_object.param1, value1); – bellotas Feb 02 '22 at 12:11
  • 1
    An object is a chunk of memory *interpreted* in a certain way. A pointer is the address of that memory, a reference is a way to use that pointer with certain guarantees. – MatG Feb 02 '22 at 12:18
  • In C++, unlike Java / C#, everything can be either a value type or a reference type. Unfortunately your use of `auto` obfuscates what that type is, we need to know what type `createMyObject`'s return value is. If `createMyObject` isn't a pointer, there's either no need to check for `NULL`/`nullptr` or you shouldn't try, because `Object{}` might not be the `null` that you are thinking, it might be a valid value (it could be an empty list, a 2-D vector initialized to `{0,0}`, an empty string, an integer with a value of 0, a giant class initialized with zeros, etc.). – jrh Feb 02 '22 at 12:31

3 Answers3

3

I want to check if the object is null.

Congratulations, your Object object is not null. null is not a possible value for object to be.

A close analog might be to test that it is different to a default-constructed Object, if Object is default-constructable.

EXPECT_TRUE(my_object != Object{})
Caleth
  • 52,200
  • 2
  • 44
  • 75
  • 2
    Note that @Caleth has buried something important deeply here. The author uses `{}` to construct the object so that all data members are initialised. Otherwise the program behaviour could be undefined during the evaluation of `!=`. Considering this must mean the answer is deserving of an upvote. One's coming... – Bathsheba Feb 02 '22 at 11:55
1
  1. nullptr is called a null pointer literal. You can also use NULL or 0 (zero), but prefer nullptr for it removes some ambiguities.
  2. You can use nullptr with pointers but not with references or objects.
  3. C++ does not initialize pointers by default. So, int* p; points to a random address (0 included).
  4. To test against null you can simply write if (!p)... or if( nullptr == p ).... Smart pointers are designed to behave in the same way as naked ones, so you can test for null in the same way.
  5. A null pointer is not necessary an error.
  6. A non-null pointer is not necessary a valid pointer - you forgot to initialize it, for instance (see 3). Another example is a pointer to an object whose storage duration has ended (@user17732522).
  7. Although references can be think as pointer in disguise, the language enforces initialization; that is they cannot point to a random location – except for this strange case: int& r = r;. The rationale behind this construct is the backward compatibility with C (I do not know if the latest standards changed this).
zdf
  • 4,382
  • 3
  • 18
  • 29
  • 2
    The wording of 3 and 6 is a bit dangerous. It makes it sound as if it is ok to test an uninitialized pointer against `nullptr`. But it isn't. A better example for an invalid pointer is a pointer to an object whose storage duration has ended. – user17732522 Feb 02 '22 at 14:22
0

Objects are never NULL. References are never NULL. std::make_shared does return not a shared pointer that compares equal to nullptr, when deallocation fails std::bad_alloc is thrown.

To check if a pointer is not nullptr you do

if ( raw_ptr != nullptr)

To check if the pointer stored by a shared pointer is not nullptr you can make use of the conversion to bool:

if ( some_shared_ptr )
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185