6

I'm having a problem with my pointers in C++, and it would be great if someone was able to share their expertise with me!

The output I'm getting is:

1:
2:
END: C
1:C
2:E
END: E

The output I was expecting is:

1:
2:
END: C
1:C
2:C
END: E

The code of relevance is this:

my test.cpp

tree.insert('C');
tree.insert('E');

The insert function:

template <typename T> pair<typename btree<T>::iterator, bool> btree<T>::insert(const T& elem) {
  cout <<  "1:" << this->rbegin_->value() << endl;
  btree_node<T> node(elem);
  cout <<  "2:" << this->rbegin_->value() << endl;
  rbegin_ = &node;
  iterator itr;
  pair<typename btree<T>::iterator, bool> p(itr, false);
  cout << "END: " << this->rbegin_->value() << endl;
  return p;
}

The constructor for btree_node (which is basically empty):

template <typename T> btree_node<T>::btree_node(const T& elem) : value_(elem), nextCont_(NULL), prevCont_(NULL), nextNode_(NULL), prevNode_(NULL) {}

The btree class has a private variable:

btree_node<T>* rbegin_;

Which is what I'm modifying. rbegin_ is initially set to an empty node in the btree constructor with:

btree_node<T> end(NULL);
rbegin_ = &end;

It seems like my node constructor, which does nothing, is modifying the value of rbegin->value()....

Any help appreciated.

Mick
  • 665
  • 2
  • 6
  • 18
  • Hope You are following the rule of Three. – Alok Save Oct 16 '11 at 12:28
  • Hi Als, sorry for my ignorance, but what is "the rule of Three"? Regards. I will definitely try to follow it, if it is indeed something reasonable to follow :) – Mick Oct 16 '11 at 16:10
  • 1
    check this out [what-is-the-rule-of-three](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) – Alok Save Oct 16 '11 at 16:11

1 Answers1

6

You got it by luck:

1:
2:
END: C
1:C     <--- Undefined.
2:E
END: E

The mistake is here:

template <typename T> pair<typename btree<T>::iterator, bool> btree<T>::insert(const T& elem) {
  cout <<  "1:" << this->rbegin_->value() << endl;
  btree_node<T> node(elem); /* LOCAL parameter, will be deleted when leaving scope*/
  cout <<  "2:" << this->rbegin_->value() << endl;
  rbegin_ = &node; /* Pointing to a LOCAL parameter, when leaving the scope it will point to undefined memory. */
  iterator itr;
  pair<typename btree<T>::iterator, bool> p(itr, false);
  cout << "END: " << this->rbegin_->value() << endl;
  return p;
}

So:
A. Allocate the memory of "node" dynamically (malloc or so).
B. I don't know what you are trying to do, but you make every insert to replace the tree head with the new value and ignoring the old head (free?)... I don't think that's want you wish to do.

Roee Gavirel
  • 18,955
  • 12
  • 67
  • 94
  • 1
    Thanks Roee, that insert function code doesn't actually do anything at the moment.. I originally had more code written, but when i noticed this bug, i went back and stripped everything away for debugging purposes. Thanks - i will try dynamically allocating node, and hopefully that works! Regards! – Mick Oct 16 '11 at 12:29
  • Thanks so much! I spent a good 3 hours wondering about this (while considering maybe some other part of my code was causing the problem). Works perfectly now. – Mick Oct 16 '11 at 12:35