首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >双重免费或损坏(out) C++

双重免费或损坏(out) C++
EN

Stack Overflow用户
提问于 2014-10-17 02:44:19
回答 1查看 6.2K关注 0票数 0

我知道双重自由或腐败错误通常是对大3的违反,但在这种情况下,我找不到违规发生的地方。对于任何处理指针的操作,我都有一个复制构造函数、析构函数和赋值操作符。

在我的.h中,这里是我的类实现:

代码语言:javascript
运行
复制
class BST
{
public:
    struct SequenceMap{
        std::string astring;
        std::vector<std::string> sequences;

        //void setValue(std::string theString, std::string anotherString);
        SequenceMap& operator=(const SequenceMap map);

        void setValue(std::string theString, std::string anotherString);

        SequenceMap(); //constructor no copy since no pointers
        ~SequenceMap();
    };
    struct BinaryNode{
        SequenceMap item;
        BinaryNode *left;
        BinaryNode *right;
        BinaryNode(SequenceMap i); //constructor

        inline bool operator> (std::string t);
        inline bool operator< (std::string t);

        BinaryNode& operator=(const BinaryNode node) ;
        ~BinaryNode();
        BinaryNode(const BinaryNode &otherNode);
    };
    BinaryNode *root;
    int insert(SequenceMap &x, BinaryNode *&t, bool &ifdup);

    BST();
    ~BST();
    void BSTClear(BST::BinaryNode *t);
    BST(const BST &otherTree);

    BST& operator=(const BST tree);
};

我在.cpp中实现了构造函数、析构函数和赋值运算符:

代码语言:javascript
运行
复制
BST::SequenceMap& BST::SequenceMap::operator=(const BST::SequenceMap map) 
{
    astring = map.astring;
    sequences = map.sequences;
    return *this;
}

inline bool BST::BinaryNode::operator<(std::string t){//does compare}
inline bool BST::BinaryNode::operator>(std::string t){//does compare}

BST::BinaryNode& BST::BinaryNode::operator=(const BST::BinaryNode node) 
{
    item = node.item;
    if(node.left != nullptr)
        left = new BST::BinaryNode(node.left->item);
    else
        left = nullptr;
    if(node.right != nullptr)
        right = new BST::BinaryNode(node.right->item);
    else
        right = nullptr;

    return *this;
}
BST& BST::operator=(const BST tree){root = new BinaryNode(tree.root);}

BST::BinaryNode::BinaryNode(const BST::BinaryNode &otherNode){
    item = otherNode.item;  
    if(otherNode.left != nullptr)
        left = new BST::BinaryNode(otherNode.left->item);
    else
        left = nullptr;
    if(otherNode.right != nullptr)
        right = new BST::BinaryNode(otherNode.right->item);
    else
        right = nullptr;
}

BST::BinaryNode::BinaryNode(SequenceMap i){ item = i; left = nullptr; right = nullptr; }
BST::BinaryNode::~BinaryNode(){ delete &item; left = nullptr; right = nullptr; }

BST::BST(){root = nullptr;}
BST::BST(const BST &otherTree){root = new BinaryNode(otherTree.root->item);}
BST::~BST(){BSTClear(root);}

BST::SequenceMap::SequenceMap(){astring = "";}
BST::SequenceMap::~SequenceMap(){ delete &astring; delete &sequences;}

void BST::BSTClear(BST::BinaryNode*t){
    if(t->left != nullptr)
        BSTClear(t->left);
    if(t->right != nullptr)
        BSTClear(t->right);      
    delete t;
}

我使用cout测试错误发生的位置,并在指定行的main.cpp中执行此操作时发生:

代码语言:javascript
运行
复制
while(getline(sequences,sequence) && getline(enzymes,enzyme))
{
    BST::SequenceMap map = BST::SequenceMap;
    map->setValue(sequence, enzyme);

    sequenceTree->insert(map, sequenceTree->root, dup); //ON THIS LINE
}

在我的.cpp中的插入函数中:

代码语言:javascript
运行
复制
int BST::insert(BST::SequenceMap &x, BST::BinaryNode *&t, bool &ifdup )
{
    if(t == nullptr)
    {
        //std::cout<<"2"<<std::endl;            
        t = new BST::BinaryNode(x); //ON THIS LINE  
        //std::cout<<"1"<<std::endl;
    }
    //do more things
 }

我不确定这是否被认为是MSCV,但我这是我至少需要重复我的错误。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-10-17 02:59:47

考虑一下您的BinaryNode赋值操作符。

代码语言:javascript
运行
复制
BST::BinaryNode& BST::BinaryNode::operator=(const BST::BinaryNode node) 
{
    item = node.item;
    if(node.left != nullptr)
        left = node.left;
    else
        left = nullptr;
    if(node.right != nullptr)
        right = node.right;
    else
        right = nullptr;

    return *this;
}

您仍然会得到BinaryNode的两个实例,它们的leftright指针指向同一事物。当调用两个实例的析构函数时,它们都将释放指针并导致双空闲。

您需要做的是实际复制leftright指针所指向的值,而不是指针,或者具有某种类型的引用计数指针。

还要注意:如果原始值为nullptr,则您的if测试不会添加任何值,因为您只是在分配nullptr

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26417088

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档