c++ - value lost after assignment in a pointer -


this code printing 0 this->min_node->right , this->min_node->left in function add_node_to_the_list. not able figure out why happening when have assigned values variables in previous iterations. value available when printing in main after calling function value lost.

#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 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 saving address of local stack variable, won't valid method returns. results undefined.

i suggest using new object in a1::heap::fib_heap_insert().


Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -