1

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");
    
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Algo
  • 185
  • 6
  • 2
    Don't use `NULL` in C++. It has been obsoleted by `nullptr`. – eerorika Apr 20 '22 at 08:50
  • Be consistent. Use `nullptr` wherever applicable instead of using `NULL`. – Jason Apr 20 '22 at 08:50
  • In C++11 NULL is defined as nullptr anyways, so straight up writing nullptr is the better solution – raizo Apr 20 '22 at 08:52
  • Check https://stackoverflow.com/a/20509811/6770517. Also better to use `if (!this) return new Node (inData);` – Ankush Apr 20 '22 at 08:53
  • 4
    Ask yourself the question "Under what circumstances would `this == nullptr` ever be true?". – G.M. Apr 20 '22 at 08:53
  • But nullptr it deosnt work , I get an error , I can't get why. – Algo Apr 20 '22 at 08:53
  • _"But nullptr it deosnt work , I get an error"_: which error do you get when you do what? – Jabberwocky Apr 20 '22 at 08:56
  • @raizo `In C++11 NULL is defined as nullptr anyways` it **may** be, but it's not guaranteed to be defined as nullptr. – eerorika Apr 20 '22 at 09:09
  • @G.M. `Node *node = nullptr; node->insert_node();`? It's UB, I know, and it's no reason to write such checks, because the bug is caused by forgetting to initialize the pointer elsewhere. Just want to say, it's not impossible to achieve. Actually I created a bug like this in the program for my master thesis. – Lukas-T Apr 20 '22 at 09:41
  • @eerorika This is true, thats why I said its better to just type out nullptr because its safer. – raizo Apr 20 '22 at 12:25

3 Answers3

4

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.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
4

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.

eerorika
  • 232,697
  • 12
  • 197
  • 326
3

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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335