Home > Enterprise >  What is the best practice to check if an object is null in C
What is the best practice to check if an object is null in C

Time:02-02

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?

CodePudding user response:

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{})

CodePudding user response:

If you want to check if your object is, in a sense, false, then build

explicit operator bool() const;

for your type. You can then use an instance in an if expression &c.:

if (my_object){

} else {
    // I am in a sense null
}

Then you don't need any macros.

CodePudding user response:

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 )

CodePudding user response:

  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( p == nullptr ).... Smart pointers are designed to behave in the same way as a 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 (see 3).
  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; (I do not know if the latest standard fixed this).
  •  Tags:  
  • Related