title: pointer referring to a garbage node
body: <p>I am generating a large game tree that depicts an auction scenario between two nodes (referred to as type1 and type2. The tree is generated perfectly fine till I reach the fourth level of nodes.          </p>
  
  <pre><code>    #include &lt;iostream&gt;
      #include &lt;vector&gt;
      #include &lt;queue&gt;
  
      #define LEVEL 8
  
      using namespace std;
  
      class payoff_node{
          int agent_1;
          int agent_2;
      };
  
      class node{
          public:
              /* constructor to set payoff_ptr to NULL */
              node() { 
                  payoff_ptr = NULL;
                  parent = NULL; 
              }
  
              /* print the information about the node */
              void printNode(){
                  cout &lt;&lt; "type: " &lt;&lt; type &lt;&lt; "\t" &lt;&lt; "level: " &lt;&lt; level &lt;&lt; "\t" &lt;&lt; "purse: $" &lt;&lt; purse &lt;&lt; "\t" &lt;&lt; "parent_bid: " &lt;&lt; parent_bid &lt;&lt; "\t" &lt;&lt; "parent:" &lt;&lt; parent-&gt;getType() &lt;&lt; "\t" &lt;&lt; endl;
                  /*for (int i = 0; i &lt; actions.size() ; i++){
                      cout &lt;&lt; actions[i] &lt;&lt; endl;
                  }*/
              }
  
              void setType(int t){    type = t;   }
  
              void setLevel(int l){   level = l;  }
  
              void setPurse(int p){ 
                  purse = p; 
  
                  /* Depending on the purse value the action space is decided */
                  for(int i = 0; i &lt;= purse ; i++){
                      actions.push_back(new node);
                  }
              }
  
              void setParentBid(int bid){ parent_bid = bid; }
  
              void setParent(node &amp;parent_node){  parent = &amp;parent_node;  }
  
  
  
              int getLevel(){ return level; }
  
              int getParentBid(){ return parent_bid;  }
  
              int getPurse(){ return purse; }
  
              node** getParent(){ return &amp;parent; }
  
              int getType(){ return type; }
  
  
              vector&lt;node*&gt; actions;
  
  
  
          private:
              int type;
              int level;
              int purse;
  
              int parent_bid;
              node* parent;
  
              payoff_node* payoff_ptr;
      };
  
      int main(int argc, char* argv[]){
          bool D = false;
          /* Make the root node and set its properties */
          node root_node;
          root_node.setType(1);
          root_node.setLevel(1);
          root_node.setPurse(4);
          root_node.setParentBid(0);
  
      //  root_node.printNode();  
          queue&lt;node&gt; myQ;
          myQ.push(root_node);
  
          /* BFS like creation of perfect information tree */
          while(true){
              node &amp;ext = myQ.front();
              if(D)
                  cout &lt;&lt; "h1" &lt;&lt; endl;
  
              if(ext.getLevel() == LEVEL){
                  break;
              }
  
              if(D)
                  cout &lt;&lt; "h2" &lt;&lt; endl;
  
              int type = ext.getType();
  
              if(D)
              cout &lt;&lt; "h3" &lt;&lt; endl;
              for(int i = 0; i &lt; ext.actions.size() ; i++){
                  node &amp;child = *(ext.actions[i]);
              if(D)
              cout &lt;&lt; "h4" &lt;&lt; endl;
                  //process(child);
  
                  /* set parent */
                  child.setParent(ext);
              if(D)
              cout &lt;&lt; "h5" &lt;&lt; endl;
  
                  /* set type  &amp; items won so far */
                  if((ext.getLevel()) % 2 == 0){    // i.e. even numbered level, then current round has ended 
                      if(i &gt; ext.getParentBid()){
                          child.setType(type);    
                      }
                      else{
                          int type_val = ( *(*(ext.getParent()))).getType() ;
                          child.setType( type_val );
                      }
                  }
                  else{
                      if(type == 1){
                          child.setType(2);
  
                      }
                      else{
                          child.setType(1);
  
                      }
                  }
  
              if(D)
              cout &lt;&lt; "h6" &lt;&lt; endl;
                  /* set level */
                  child.setLevel(ext.getLevel() + 1);
              if(D)
              cout &lt;&lt; "h7" &lt;&lt; endl;
                  /* set purse */
                  if(child.getType() == ext.getType()){
                      int val = ext.getPurse() - i;
                      if(val &lt; 0){
                          child.setPurse(0);
                      }
                      else{
                          child.setPurse(val);
                      }
                  }
                  else{
  
                      if( *(ext.getParent()) != NULL  ){
                          int val = ( *(ext.getParent()))-&gt;getPurse()  - ext.getParentBid();
                          if(val &lt; 0){
                              child.setPurse(0);
                          }
                          else{
                              child.setPurse( val );
                          }
                      }
                      else{
                          child.setPurse(3);
                      }
                  }
              if(D)
              cout &lt;&lt; "h8" &lt;&lt; endl;
  
                  /* set parent bid */
                  child.setParentBid(i);
  
  
              if(D)
              cout &lt;&lt; "h9" &lt;&lt; endl;
                  /* when the child is ready with all its attributes, add it to the queue */
                  myQ.push(child);
                  child.printNode();
              }
  
              cout &lt;&lt; endl &lt;&lt; endl &lt;&lt; "----Next Set of Children----" &lt;&lt; endl;
              myQ.pop();
  
          }
  
  
  
      return 0;
  
      }
  </code></pre>
  
  <p>The program hangs at this line <code>child.setPurse( val );</code>I believe the value calculated by the following line</p>
  
  <pre><code>    int val = ( *(ext.getParent()))-&gt;getPurse()  - ext.getParentBid();
  </code></pre>
  
  <p>is wrong. Where the <code>*(ext.getParent())</code> points to some garbage node. Any help will be much appreciated as I am not being able to figure this out for over 24 hours now. Thank you.</p>