free():无效指针:在C ++中

时间:2015-07-10 05:26:10

标签: c++ tree

我制作了树数据结构,但是当我擦除对象时我遇到了问题 问题内容如下:

*`./t'出错:free():无效指针:0x00007ffd1df99400 *

중지됨(核心倾销)

源代码

的main.cpp

#include<iostream>
#include"tree.h"
#include"point.h"


using namespace std;

template<class T>
void ShowIntData(Point<T> data)
{
    cout<<data.x<<" "<<data.y<<endl;
}

typedef Point<double> pd;

int main(void)
{
    BTreeNode<pd> bt1; bt1.make();
    BTreeNode<pd> bt2; bt2.make();
    BTreeNode<pd> bt3; bt3.make();
    BTreeNode<pd> bt4; bt4.make();

    bt1.SetData(pd(1,10));
    bt2.SetData(pd(2,20));
    bt3.SetData(pd(3,30));
    bt4.SetData(pd(4,40));

    bt1.MakeLeftSubTree(&bt2);
    bt1.MakeRightSubTree(&bt3);
    bt2.MakeLeftSubTree(&bt4);

    bt1.InorderTraverse(ShowIntData);
    cout<<endl;

    bt1.PreorderTraverse(ShowIntData);
    cout<<endl;

    bt1.PostorderTraverse(ShowIntData);
    cout<<endl;

    bt1.DeleteTree(); // Error occurred
    return 0;
}

point.h

#ifndef __T_POINT_H__
#define __T_POINT_H__

template<class T>
struct Point
{
    T x;
    T y;
    Point() : x(0),y(0){}
    Point(T x,T y) : x(x),y(y) {}
};

#endif

tree.h中

#ifndef __B_TREE_H__
#define __B_TREE_H__
#include<iostream>
#include<cstdlib>
using namespace std;

template<class T>
class BTreeNode
{
    private:
        T data;
        BTreeNode<T>* left;
        BTreeNode<T>* right;
    public:
    typedef void VisitFuncPtr(T data);
        BTreeNode();
        BTreeNode<T>* make();
        ~BTreeNode();
        T GetData();
        void SetData(T data);
        BTreeNode<T>* GetLeftSubTree();
        BTreeNode<T>* GetRightSubTree();
        void MakeLeftSubTree(BTreeNode<T>* sub);
        void MakeRightSubTree(BTreeNode<T>* sub);
        void DeleteTree();

        void InorderTraverse(VisitFuncPtr action);  //중위 순회
        void PreorderTraverse(VisitFuncPtr action); //전위 순회
        void PostorderTraverse(VisitFuncPtr action); // 후위 순회

};

template<class T>
BTreeNode<T>::BTreeNode()
{
    left = NULL;
    right = NULL;
}

template<class T>
BTreeNode<T>::~BTreeNode()
{
    cout<<"Destructor"<<endl;
}


template<class T>
BTreeNode<T>* BTreeNode<T>::make(void)
{
    left = NULL;
    right = NULL;
    return this;
}

template<class T>
T BTreeNode<T>::GetData()
{
    return data;
}

template<class T>
void BTreeNode<T>::SetData(T data)
{
    this->data = data;
}

template<class T>
BTreeNode<T>* BTreeNode<T>::GetLeftSubTree()
{
    return left;
}

template<class T>
BTreeNode<T>* BTreeNode<T>::GetRightSubTree()
{
    return right;
}

template<class T>
void BTreeNode<T>::MakeLeftSubTree(BTreeNode<T>* sub)
{
    if(this->left != NULL)
        delete this->left;
    this->left = sub;
}

template<class T>
void BTreeNode<T>::MakeRightSubTree(BTreeNode<T>* sub)
{
    if(this->right != NULL)
        delete this->right;
    this->right = sub;
}

template<class T>
void BTreeNode<T>::InorderTraverse(VisitFuncPtr action)
{
    if(this==NULL)
        return;

    left->InorderTraverse(action);
    action(data);
    right->InorderTraverse(action);

}

template<class T>
void BTreeNode<T>::PreorderTraverse(VisitFuncPtr action)
{
    if(this==NULL)
        return;
    action(data);
    left->PreorderTraverse(action);
    right->PreorderTraverse(action);
}

template<class T>
void BTreeNode<T>::PostorderTraverse(VisitFuncPtr action)
{
    if(this==NULL)
        return;
    left->PostorderTraverse(action);
    right->PostorderTraverse(action);
    action(data);
}

template<class T>
void BTreeNode<T>::DeleteTree()
{
    if(this==NULL)
        return;

    left->DeleteTree();
    right->DeleteTree();

    delete this; //Error occurred


}


#endif

1 个答案:

答案 0 :(得分:2)

delete必须仅用于已使用new在堆上分配的对象。

在你的代码中,所有4个节点都已在堆栈上分配,因此调用它们上的delete是一个无效的操作,导致&#34;未定义的行为&#34;。