簡體   English   中英

為什么打印指針地址會觸發斷點?

[英]Why does printing a pointer address trigger a breakpoint?

我正在嘗試實現用於學習目的的二叉樹。 當我第一次遇到錯誤時,我以為可能是兩次刪除一個節點。 然后我意識到,即使print語句也會觸發斷點。 整個程序中的其他任何地方都沒有刪除,因此問題必須在這里。

這是BinaryTree.cpp

#include "BinaryTree.h"
#include<iostream>

BinaryTree::BinaryTree(void):root(nullptr){}
BinaryTree::~BinaryTree(void){removeWithChildren(root);}

void BinaryTree::insert(Node *n){
    cout<<"\nInserting: "<<(void*)n;
    Node *y = nullptr;
    Node *x = root;
    while(x != nullptr){
        y = x;
        if(n->key < x->key)
            x = x->left;
        else
            x = x->right;
    }
    n->parent = y;
    if(y == nullptr)
        root = n;
    else if (n->key < y->key)
        y->left = n;
    else
        y->right = n;
}

void BinaryTree::removeWithChildren(Node *n){   
//forgetChild(n);
    if(n->left)
        removeWithChildren(n->left);
    if(n->right)
        removeWithChildren(n->right);
    cout<<"\nDeleting: "<<(void*)n;
    delete n;

}
void BinaryTree::remove(Node *n){
    if(n->left == nullptr) {
        transplant(n,n->right);     
    } else if(n->right == nullptr) {
        transplant(n,n->left);
    } else {
        Node *y = minimum(n->right);
        if(y->parent != n){
            transplant(y,y->right);
            y->right = n->right;
            y->left = n->left;          
        }
        transplant(n,y);
        y->left = n->left;
        y->left->parent = y;
    }
    cout<<"\nDeleting: "<<(void*)n;
    delete n;
}
void BinaryTree::transplant(Node *u,Node *v){
    if(u->parent == nullptr) root = v;
    else if (u == u->parent->left) u->parent->left = v;
    else u->parent->right = v;
    if(v) v->parent == u->parent;   
}
string BinaryTree::prewalk(Node *n){
    string output = "";
    if(n!=nullptr){
        output += prewalk(n->left);
        output += prewalk(n->right);
        output += to_string(n->key);
    }
    return output;
}

string BinaryTree::inwalk(Node *n){
    string output = "";
    if(n!=nullptr){
        output += inwalk(n->left);
        output += to_string(n->key);
        output += inwalk(n->right);
    }
    return output;
}

Node* BinaryTree::search(Node *sub_tree,int key){
    if(sub_tree == nullptr) return nullptr;
    if(sub_tree->key == key) return sub_tree;
    if(sub_tree->key < key) return search(sub_tree->right,key);
    else return search(sub_tree->left,key);
}

Node* BinaryTree::getSuccessor(Node *n){
    if(n->right)
        return minimum(n->right);
    Node *y = n->parent;

    while(y){
        if(n != y->right) break;
        n = y;
        y = y -> parent;
    }
    return y;
}
Node* BinaryTree::minimum(Node *sub_tree){
    while(sub_tree->left)
        sub_tree = sub_tree ->left;
    return sub_tree;
}
Node* BinaryTree::maximum(Node *sub_tree){
    while(sub_tree->right)
        sub_tree = sub_tree ->right;
    return sub_tree;
}
void BinaryTree::forgetChild(Node *n){
    if(n->parent){
        if(n == n->parent->left) n->parent->left = nullptr;
        else n->parent->right = nullptr;
    }
}

這是main.cpp

#include"BinaryTree.h"

#include<iostream>
#include<random>

using namespace std;

int main(){
    {
        BinaryTree bt;

        bt.insert(new Node(5));
        bt.insert(new Node(1));
        bt.insert(new Node(3));
        bt.insert(new Node(4));
        bt.insert(new Node(9));

        //cout<<bt.inwalk(bt.getRoot())<<endl;

        bt.remove(bt.search(bt.getRoot(),3));

        //cout<<bt.inwalk(bt.getRoot())<<endl;
    }
    char x;cin>>x;
    return 0;
}

這是BinaryTree.h

#pragma once
#include<string>

using namespace std;

struct Node{
    Node *left,*right,*parent;
    int key;
    Node():left(nullptr),right(nullptr),parent(nullptr),key(0) {}
    Node(int x):left(nullptr),right(nullptr),parent(nullptr),key(x) {}
};

class BinaryTree
{
private:
    Node *root; 

public:
    BinaryTree(void);
    ~BinaryTree(void);
    Node* getRoot() { return root; }

    void insert(Node *n);
    void removeWithChildren(Node *n);
    void remove(Node *n);
    string prewalk(Node *n);
    string inwalk(Node *n);

    Node* search(Node *sub_tree,int key);
    Node* minimum(Node *sub_tree);
    Node* maximum(Node *sub_tree);
    Node* getSuccessor(Node *n);
    void forgetChild(Node *n);
    void transplant(Node* u,Node*v);

};

析構函數調用removeWithChildren(Node *n)函數,並且參數是樹的根。

我一次調用remove(Node *n) 當我不打電話時,沒有錯誤。 我單步執行並檢查了代碼, removeWithChildren函數未嘗試刪除由remove函數刪除的節點。 仍然有一個錯誤。

編輯:我在Microsoft Visual Studio 2012速成版上。 我不知道哪種斷點。 斷點

EDIT2:注釋掉forgetChildremoveWithChildren修正錯誤的某些原因。

您沒有發布代碼的相關部分,但是您發布的內容導致您猜測既未提供有效的副本構造函數,也未提供任何防止使用默認副本構造函數的猜測。 然后,您犯了一個錯誤,即按值傳遞對象(應按引用傳遞時),從而調用了默認的復制構造函數和析構函數,並破壞了內存。

當使用難以復制且可能不需要復制的類時,請確保無法使用默認的復制構造函數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM