指针赋值后丢失的值
value lost after assignment in a pointer
此代码为add_node_to_the_list
函数中的this->min_node->right
和this->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
。
相关文章:
- 全局作用域中函数指针的赋值
- 错误:在为指针赋值时,void值没有被忽略
- 关于 c++ 函数中指针赋值的简单问题
- VkSurfaceKHR 指针的值在函数调用后更改,无需任何显式赋值
- 在 Arduino 中为 2D 指针数组赋值
- 链表指针赋值为什么我们不能直接将尾巴分配给 temp 而不是尾巴>尾巴下一个
- 为什么通过指针赋值常量没有效果?
- 通过 lambda 在空指针处赋值
- 指针到指针算术:赋值
- 复制构造函数中的指针赋值
- 如何将值从指针永久赋值到变量
- 通过智能指针向类成员进行赋值
- 为什么在取消引用的指向接口的指针上使用赋值运算符不是编译器错误
- 将函数赋值给函数指针,常量参数正确性
- 为指针指向的地址赋值?C++
- C++ 包含唯一指针成员变量的类的赋值运算符
- 在类数据成员中赋值指针
- 有没有一种方法可以自动地赋值指针并增加计数器,而不需要锁?
- 赋值指针数组
- 如何在c++中赋值指针给this