指针赋值后丢失的值

value lost after assignment in a pointer

本文关键字:赋值 指针      更新时间:2023-10-16

此代码为add_node_to_the_list函数中的this->min_node->rightthis->min_node->left打印0。我不能弄清楚为什么它发生的时候,我已经分配了一些值到这个变量在以前的迭代。当我在main中打印它时,这个值是可用的,但是在调用函数后,这个值丢失了。

#include<iostream>
#include<vector>
#include<iterator>
namespace a1{
/******************************Declarations*********************************/
class node{
    //Private member declaration
    //Public member declaration
    public: int key;
    public: int degree;
    public: bool mark;
    public: node * left;
    public: node * right;
    public: node * child;
    public: node * parent;
    //Private method declaration
    //Public method declaration
    public: node(int);
};
class heap{
    //Private member declaration
    private: int node_count;
    private: void add_node_to_the_list(node *);
    //Public member declaration
    public: node * min_node;
    //Private method declaration
    private: void consolidate();
    private: void fib_heap_link(node *, node *);
    private: void cut(node *, node *);
    private: void cascading_cut(node *);
    //Public method declaration
    public: heap();
    public: void fib_heap_insert(int);
    public: void fib_heap_union(heap &);
    public: heap & fib_heap_extract_min();
    public: void fib_heap_decrease_key(node *, int);
    public: void fib_heap_delete(node *);
    public: int get_node_count();
};
};//End of namespace a1
/****************************Definitions*************************************/
/****************************node functions here*****************************/
a1::node::node(int key){
    this->key       = key;
    this->degree    = 0;
    this->mark      = false;
    this->left      = NULL;
    this->right     = NULL;
    this->child     = NULL;
    this->parent    = NULL;
}

/****************************Heap functions here*****************************/
//Private methods
void a1::heap::add_node_to_the_list(node * temp){
    if(this->min_node == NULL){
        this->min_node = temp;
        this->min_node->right = this->min_node;
        this->min_node->left = this->min_node;
    }
    else{
        temp->right = this->min_node->right;
        temp->left = this->min_node;
        //this->min_node->right->left = temp;
        //this->min_node->right = temp;
    }
}
//Public methods
a1::heap::heap(){
    this->node_count = 0;
    this->min_node = NULL;
}
void a1::heap::fib_heap_insert(int key){
    a1::node temp(key);
    if(this->min_node == NULL){
        a1::heap::add_node_to_the_list(&temp);
        this->min_node = &temp;
    }
    else{
        std::cout << "Printing this->min_node->right : " << this->min_node->right << std::endl;
        std::cout << "Printing this->min_node->left : " << this->min_node->left << std::endl;
        a1::heap::add_node_to_the_list(&temp);
        if(this->min_node->key > temp.key){
            this->min_node = &temp;
        }
    }
    this->node_count += 1;
}
int a1::heap::get_node_count(){
    return this->node_count;
}

/**************************Debug functions***********************************/
using namespace a1;
void print_fib_heap(node * n){
    if(n == NULL) return;
    else{
        node * min_node;
        node * trav;
        bool flag = false;
        min_node = n;
        trav = n;
        while(flag == false || trav != min_node){
            flag = true;
            std::cout << "(" << "key = " << trav->key << " d = " << trav->degree;
            print_fib_heap(trav->child);
            std::cout << ")";
            trav = trav->right;
        }
    }
}
/**************************Main Function to test*****************************/
int main(){
    heap h1;
    h1.fib_heap_insert(2);
    std::cout << "From main h1.min_node->right: " << h1.min_node->right << std::endl;
    h1.fib_heap_insert(3);
    //print_fib_heap(h1.min_node);
    return 0;
}

this->min_node = &temp正在保存本地堆栈变量的地址,该地址在方法返回时将无效。那么结果将是未定义的。

我建议在a1::heap::fib_heap_insert()中使用new