Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么在空检查后会对指针调用递归函数? //因此我在删除节点后调用此函数。 //它在我删除节点之前工作,但出于某种原因 //在我执行删除并更新它运行到的树之后 //EXC\u在下面的行中访问错误\u。。。 void BinaryTree::updateCost(BinaryNode*root){ if(root!=NULL) 根->临时更新(1); } void BinaryNode::updateConstrevery(int newCost){ 不能临时更新(新成本+1); }_C++_Pointers_Memory Management - Fatal编程技术网

C++ 为什么在空检查后会对指针调用递归函数? //因此我在删除节点后调用此函数。 //它在我删除节点之前工作,但出于某种原因 //在我执行删除并更新它运行到的树之后 //EXC\u在下面的行中访问错误\u。。。 void BinaryTree::updateCost(BinaryNode*root){ if(root!=NULL) 根->临时更新(1); } void BinaryNode::updateConstrevery(int newCost){ 不能临时更新(新成本+1); }

C++ 为什么在空检查后会对指针调用递归函数? //因此我在删除节点后调用此函数。 //它在我删除节点之前工作,但出于某种原因 //在我执行删除并更新它运行到的树之后 //EXC\u在下面的行中访问错误\u。。。 void BinaryTree::updateCost(BinaryNode*root){ if(root!=NULL) 根->临时更新(1); } void BinaryNode::updateConstrevery(int newCost){ 不能临时更新(新成本+1); },c++,pointers,memory-management,C++,Pointers,Memory Management,为什么每次检查指针时都要对NULL对象调用这个递归函数 我已复制了用于删除以下节点的代码。我在理解递归函数方面仍然有困难,但是从我所知道的情况来看,我在任何时候都不会留下一个悬而未决的指针 // So I call this function after deleting a node. // It works before I delete the node, but for some reason // after I perform a deletion and update the tr

为什么每次检查指针时都要对NULL对象调用这个递归函数

我已复制了用于删除以下节点的代码。我在理解递归函数方面仍然有困难,但是从我所知道的情况来看,我在任何时候都不会留下一个悬而未决的指针

// So I call this function after deleting a node.
// It works before I delete the node, but for some reason
// after I perform a deletion and update the tree it runs into
// EXC_BAD_ACCESS on the line below...

void BinaryTree::updateCost(BinaryNode *root) {
    if (root != NULL)
        root->updateCostRecursively(1);
}

void BinaryNode::updateCostRecursively(int newCost) {
    cout << this << endl; // prints 0x3000000000000000 before the bad access
    cost = newCost; // has a bad access here
    if (right != NULL)
        right->updateCostRecursively(newCost + 1);
    if (left != NULL)
        left->updateCostRecursively(newCost + 1);
}
BinaryNode*BinaryTree::findMin(BinaryNode*t){
如果(t==NULL)返回NULL;
而(t->left!=NULL)t=t->left;
返回t;
}
BinaryNode*BinaryTree::removeMin(BinaryNode*t){
如果(t==NULL)返回NULL;
如果(t->left!=NULL)
t->left=removeMin(t->left);
否则{
BinaryNode*node=t;
t=t->右;
删除节点;
}
返回t;
}
bool二进制树::删除(int键){
if(root!=NULL&&remove(key,root))
返回true;
返回false;
}
BinaryNode*BinaryTree::remove(int x,BinaryNode*t){
如果(t==NULL)返回NULL;
如果(x键)
t->left=移除(x,t->left);
否则如果(x>t->键)
t->right=移除(x,t->right);
如果(t->left!=NULL&&t->right!=NULL),则为else{
//找到项目x;t有两个子项
t->key=findMin(t->right)->key;
t->right=removeMin(t->right);
}否则{//t只有一个孩子
BinaryNode*node=t;
t=(t->left!=NULL)?t->left:t->right;
删除节点;
}
更新成本(根);
返回t;
}

错误在您的删除方法中,而不是您发布的代码中。删除节点后(比如
root->right
),需要设置
root->right=NULL
。使用
delete
所做的一切就是释放指针指向的内存。指针本身继续指向该地址。由于您试图访问已释放的内存,因此出现了错误的访问异常。

因为它不是空的?请注意,
delete
不会修改指针。如果这不是问题所在,那么请构造一个.0x30000000000!=0我已粘贴用于删除节点的函数。据我所知,我没有留下一个悬空的指针,但我可能错了。@Yep:我建议你用调试器仔细检查你的
remove
函数,以确认这一点。@Yep很抱歉,我现在没有时间仔细看那段代码。乍一看,如果你删除一片叶子,你会看到一个悬空的指针。好的,我会尝试这样做。谢谢。我不确定这是否是正确的位置,但我相信这是我的析构函数递归删除其子项的问题,这当然产生了可怕的结果。
BinaryNode *BinaryTree::findMin(BinaryNode *t) {
    if (t == NULL) return NULL;
    while (t->left != NULL) t = t->left;
    return t;
}

BinaryNode *BinaryTree::removeMin(BinaryNode *t) {
    if (t == NULL) return NULL;
    if (t->left != NULL)
        t->left = removeMin(t->left);
    else {
        BinaryNode *node = t;
        t = t->right;
        delete node;
    }
    return t;
}

bool BinaryTree::remove(int key) {
    if (root != NULL && remove(key, root))
        return true;
    return false;
}

BinaryNode *BinaryTree::remove(int x, BinaryNode *t) {
    if (t == NULL) return NULL;

    if (x < t->key)
        t->left = remove(x, t->left);
    else if (x > t->key)
        t->right = remove(x, t->right);
    else if (t->left != NULL && t->right != NULL) {
        // item x is found; t has two children
        t->key = findMin(t->right)->key;
        t->right = removeMin(t->right);
    } else { //t has only one child
        BinaryNode *node = t;       
        t = (t->left != NULL) ? t->left : t->right;
        delete node;
    }

    updateCost(root);
    return t;
}