在指针中赋值后丢失的值

时间:2015-08-16 17:54:30

标签: c++ pointers

此代码正在函数0中打印this->min_node->rightthis->min_node->left add_node_to_the_list。当我在之前的迭代中为这些变量分配了一些值时,我无法弄清楚为什么会发生这种情况。当我在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;
}

1 个答案:

答案 0 :(得分:2)

this->min_node = &temp正在保存本地堆栈变量的地址,一旦该方法返回,该变量就不会有效。结果将是未定义的。

我建议在new中使用a1::heap::fib_heap_insert()作为该对象。