An error while compare pointer with "NULL"

1.2k views Asked by At

I got an error while "if (this == nullptr)" , the error :

 warning: nonnull argument 'this' compared to NULL [-Wnonnull-compare]

22 | if (this != nullptr)

The command : g++ -std=c++11 -DNDEBUG -Wall .\avl.cpp

Why can't I compare pointer with nullptr ? (null doesn't work either). How am I supposed to check if the pointer is null?

The code :

#include <iostream>
#include <stdlib.h>

namespace AVL{
    template <class T>
    class Node{
        
        

        public:
        T data;
        Node<T> *left,*right;
        int height;
        ~Node(){
            delete left;
            delete right;
        }
        Node(T& inData):data(inData),left(NULL),right(NULL),height(1){};

        Node<T> * insert_node (T & inData)
        {
            if (this == nullptr)
                return new Node<T> (inData);

            if (inData < data)
                left = left -> insert_node (inData);
            else
                right = right -> insert_node (inData);
            return this;
        }
        private:
        
        
        void compute_height(){
            height=0;
            if(left != NULL && left->height > height){height = left->height;}
            if(right != NULL && right->height > height){height = right->height;}
        }

        
        
      

};
}

int main ()
{
    int num1=55,num2=76;
    AVL::Node<int> *new_node1 = new AVL::Node<int>(num1);
    AVL::Node<int> *new_node2 = new AVL::Node<int>(num2);
    new_node2=new_node1->insert_node(num2);
    system("pause");
    
}
3

There are 3 answers

0
463035818_is_not_an_ai On

First, it is a warning, not an error:

warning: nonnull argument 'this' compared to NULL [-Wnonnull-compare]
   22 |             if (this == 0)

The compiler tries to tell you that it makes no sense to compare this to NULL (or 0 or nullptr), because this == 0 or this == nullptr or this == NULL is never true.

this is never 0, NULL or nullptr. The condition can and should be removed.


Why can't I compare pointer with nullptr ?

You can compare this with nullptr or 0. It just doesn't make sense because the result is always false.

How am I supposed to check if the pointer is null?

You are not supposed to check if this is null, because that never happens. It's not quite clear what purpose that if is supposed to have in your code. If you expected it to catch errors like this:

Node* n = nullptr; 
new_node = n->insert_node(num2);  // UB!!

Then you were wrong. Dereferencing a null pointer is undefined behavior. You cannot call a member function on a nullpointer, hence inside a member function this is never nullptr.

0
Vlad from Moscow On

insert_node is a non-static member function. It may be called for an existent object. So this can not be a null pointer. As a result the if statement does not make a sense.

    Node<T> * insert_node (T & inData)
    {
        if (this == nullptr)
            return new Node<T> (inData);
            //...

Moreover this code snippet

AVL::Node<int> *new_node2 = new AVL::Node<int>(num2);
new_node2=new_node1->insert_node(num2);

produces a memory leak.

0
eerorika On

I got an error ...

warning:

That's not an error. That's a warning.

Why can't I compare pointer with nullptr ?

You can compare the pointer with null. It's just pointless in this case because this is never null. It's like comparing whether unsigned integer is >= 0 or to test a_bool == true || a_bool == false. There is no case where the test would be false.

That is why the compiler is being helpful and warns that you wrote something that doesn't make sense. Think about the reason why you thought it would be useful to write that check, and then compare that reason with the fact that this can never be null. There's probably a more serious bug than redundant test...

And here is the serious bug:

left = left -> insert_node (inData);

You never test whether left is null before indirecting through it. The consequence is that the behaviour of the program is be undefined. Don't do this. You may indirect only through valid pointers.

You can also find this bug easily using an address sanitiser.