title: Memory Allocation Failure in C++(using new & delete)
body: <p>I am an ordinary student trying to code a Monte-Carlo Tree Search.</p>
  
  <p>To do this, I made a struct named "Node", and tried to make new Node* pointers and allocate memory to them with "new", but my program keeps on crashing, and I would reeeeally appreciate some help. </p>
  
  <p>Here's what my code looks like. Locations of failure are marked. </p>
  
  <p>Thank you in advance.</p>
  
  <pre><code>Node* knell;
  Node* treePolicy(Node* currentnode){
      puts("treepolicy");
      Node* temp=(Node*)malloc(sizeof(Node));
      puts("Mem Allocated");
      Move save=MCTBoard.generateRandomLegalMove(currentnode-&gt;player);      ///works very well up to here.
      save.printMove();
      temp=findNode(currentnode,save);
      puts("Node Found");
      knell=new Node; 
      if(temp==NULL){
          free(temp);
          temp = new Node; ///crashes the second time treePolicy(Node*) is called.
          temp-&gt;setMove(save);
          temp-&gt;child.clear();
          currentnode-&gt;child.push_back(temp);
          temp-&gt;parent=currentnode;
          MCTBoard.playMove(save,currentnode-&gt;player);
          for(int i=0;i&lt;4;i++){
              for(int j=0;j&lt;=i;j++){
                  for(int k=0;k&lt;=i;k++){
                      temp-&gt;board[i][j][k]=MCTBoard.a[i][j][k];
                  }
              }
          }
          temp-&gt;value=temp-&gt;visited=temp-&gt;win=temp-&gt;draw=0;
          temp-&gt;player=3-currentnode-&gt;player;
          knell=temp;
          //delete temp; -&gt; even with this enabled, still crashes.
          return knell;///an infinite loop happens here, but that is another problem so...
      }
  else{
      ///not important,and besides,I've not even reached here yet once.
  }
  </code></pre>
  
  <p>}</p>
  
  <p>actually there does exist a same problem in another function, but I feel that that is some other problem.</p>
  
  <p>So, can anyone please tell me why it crashes??</p>