奇怪的隐式内存管理行为

时间:2017-11-14 10:24:47

标签: c++ templates memory-management

我已经根据Cormen的书籍伪代码绘制了红黑平衡二叉树中节点的旋转过程。

N是具有上一个节点(p),左子(left)和右子(right)字段的树节点模板类。 nil是RB树的通用空指针的特殊表示,RB树是N类的对象。 当我添加3个节点并按顺序增加键到我的树时,调用此过程来平衡树。我在第11行收到错误,因为第9行中的if情况被忽略,程序试图访问(x->p->left)值而x->p具有NULL值。使用调试器后我发现第8行x开始指向nil值。

template <typename K, typename V, typename N>
void TreeMap<K, V, N>::leftRotate(N *&x) {
    N *y = x->right;
    x->right = y->left;

    if (y->left != nil)
        y->left->p = x;
    y->p = x->p; // line 8
    if ((x->p) == nil) // line 9
        root = y;
    else if (x == (x->p->left)) // line 11
        x->p->left = y;
    else
        x->p->right = y;
    y->left = x;
    x->p = y;
}

然后我添加了x的临时指针,并使用它代替x,并根据需要执行代码。

template <typename K, typename V, typename N>
void TreeMap<K, V, N>::leftRotate(N *&x) {
    N *y = x->right;
    x->right = y->left;

    if (y->left != nil)
        y->left->p = x;
    N *temp = x;
    y->p = x->p;
    if ((temp->p) == nil)
        root = y;
    else if (temp == (temp->p->left))
        temp->p->left = y;
    else
        temp->p->right = y;
    y->left = temp;
    temp->p = y;
} 

请你解释一下这种行为。我假设这是因为在第8行之后没有任何内容保持对x的引用除了x本身并且内存被隐式释放,但我不明白为什么简单的指针解决了这个局面。

0 个答案:

没有答案
相关问题