0

I am traying to code an AVL Tree class in c++ that supports the normal operations for homework. while trying code the insert member function I found out that when I debug and put a break point in this member function the debugger goes crazy. to make sure it is not something that I code wrong I changed the code to this simple one to see what happens:

template<class X,class Y>
void AvlTree<X,Y>::insert(const X& key,const Y& data)
{
    if(this==nullptr)
    {
        std::cout<<"I am null"<<std::endl;
    }
    std::cout<<"insorted"<<std::endl;
}  

It turns out that while running the program:

int main()
{
   AvlTree<int,int> tree;
   tree.insert(1,1);
}

the insert function prints "insorted" but while debugging the program and putting a break point on the <<"insorted"<< it prints "I am null" a lot of times. I don't really get why the this pointer is null while calling it on a non null tree. is someone knows what seems to be the problem?

tomer
  • 11
  • 2
  • If this==nullptr you might have a serious problem. – Surt Dec 02 '20 at 21:56
  • I agree with the previous comment. Without the rest of the class implementation, it's difficult to understand what is happening. The only way a this pointer can be null is through [undefined behavior](https://stackoverflow.com/questions/31336294/in-which-case-the-c-this-pointer-can-be-null). – WaterFox Dec 02 '20 at 22:55
  • `this==nullptr` means your program has done something highly illegal. You need to stop and find out what it was. Use a debugger. – n. m. could be an AI Dec 02 '20 at 23:13

0 Answers0