0

I am trying to implement the insert function for a pointer heap. Each node ("BinaryNode") stores an int and a string. My insert function is based on the explanation here. However, when I use this function to create a binary tree, it seems like I have a memory leak when it goes into the branch

else {
   BinaryNode *curr;//potential problem
...}

For example, when I insert 1, 2, 3, 4, 5 (in that order), I want the tree to look like the tree here. Below is my insert function.

void MyTree::insert (int x ,string s) {
   ++size;
   BinaryNode *pointer = new BinaryNode(s,x);
   if (root == NULL) {
      root = pointer;
      tail = root;
      return;
   }
   else if (tail == root) {
      tail = pointer;
      tail->parent = root;
      root->lchild = tail;
   }
   else if (tail == tail->parent->lchild) {
      tail->parent->rchild = pointer;
      pointer->parent = tail->parent;
      tail = pointer;
   }
   else {
      BinaryNode *curr;//potential problem
      for (curr = tail; curr->parent != NULL && curr == curr->parent->rchild; curr = curr->parent) {
         continue;
         if (curr == root)
            break;
      }
      if (curr != root) {
         curr = curr->parent->rchild;
      }
      while (curr->lchild != NULL) {
         curr = curr->lchild;
      }
      cout << "curr: " << curr->getInt();
      curr->lchild = pointer;
      pointer->parent = curr;
      tail = pointer;
   }
return;
}

I realized the problem when I try to store a vector of BinaryNodes. When I try to push back the node containing 3 as the int, I get the following error:

*** Error in `./a.out': free(): invalid pointer: 0x00000000021cb010 ***
======= Backtrace: =========
...

To recap, calling the insert function doesn't give me any errors, but trying to store the node containing 3 in a vector gives the above error.

If my insert function looks fine, then I'll know to look at another function. Any insights are appreciated!

trincot
  • 317,000
  • 35
  • 244
  • 286
  • 2
    Please take [the tour](https://stackoverflow.com/tour), read [the help page](https://stackoverflow.com/help) and provide a [mcve]. Welcome to SO. As-is we can only guess what the `MyTree` and `BinaryNode` are and what the actual question is. Every `new` should be accompanied by `delete`. Use smart pointers instead. There are ready-made containers in Standard C++ Library so no need to wire the structures together. – Ron Feb 28 '18 at 07:50
  • `for (curr = tail;...` What is the value of `tail`? Is it guaranteed to be initialized or is it just set to the result of `new BinaryNode(s,x);` that isn't included in the question? – David C. Rankin Feb 28 '18 at 08:11
  • 1
    @Ron: There's no pure tree container, though. But I agree with your statement on smart pointers. Use the highest level of existing building blocks when building something new. That includes providing an iterator interface for your binary tree so the whole of `` is usable. – MSalters Feb 28 '18 at 09:16
  • 1
    Welcome to Stack Overflow! Please [edit] your question to show us what kind of debugging you've done. I expect you to have run your [mcve] within Valgrind or a similar checker, and to have investigated with a debugger such as GDB, for example. Ensure you've enabled a full set of compiler warnings, too. What did the tools tell you, and what information are they missing? And read Eric Lippert's [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Toby Speight Feb 28 '18 at 09:34
  • I cannot be certain what pointer, root, and tail are. Please include your constructor(s) and struct declaration. – Aleka Feb 28 '18 at 09:35

0 Answers0