jeudi 28 mars 2019

Need help fixing "pointer being freed was not allocated" error

I'm implementing a Rope data structure for a homework assignment and I'm having problems with my destructor. I'm getting an malloc: *** error for object 0x10056ce50: pointer being freed was not allocated error. I went through the function calls and it doesn't seem like the delete operator's being called twice. Thanx for any help! Code for the Rope destructor and Rope nodes is below:

class Node{
public:
    Node() : left{NULL}, right{NULL}, parent{NULL}, weight{0} {}
    Node* left;
    Node* right;
    Node* parent;
    int weight;
    std::string value;
};

void Rope::destroy_rope(Node* p) {
  if (p) {
    destroy_rope(p->left);
    destroy_rope(p->right);
    delete p; # error comes from this statement
  }
  size_ = 0;
  p = NULL;
}

Rope::~Rope(){
  destroy_rope(root);
}

Aucun commentaire:

Enregistrer un commentaire