title: C++/g++: How does the compiler handle memory-allocation in this situation?
body: <p>Is there a difference in how g++ handles this situation because of the differnece in code? To a beginner it seems like exactly the same code tbh.</p>
  
  <p>I should mention, that both trees are massive in size, each containing around ten or so tensors with each an estimated element count of 10^5 or so integers. </p>
  
  <p>EDIT: All numbers are allocated to the heap, with only one pointer from the tree-root actually laying on the stack.</p>
  
  <pre><code>{
          std::cout &lt;&lt; "\nTrial #" &lt;&lt; i &lt;&lt; std::endl;
          v = createV(10, 5, 10);    
  
          ExTree&lt;int&gt; treeOpt = build_opt(v);
          {
              //...
              treeOpt.evaluate();
          }
          ExTree&lt;int&gt; treeNai = build_naive(v);
          {
              //...
              treeNai.evaluate();
          }
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>{
          std::cout &lt;&lt; "\nTrial #" &lt;&lt; i &lt;&lt; std::endl;
          v = createV(10, 5, 10);    
          ExTree&lt;int&gt; treeNai = build_naive(v);
          ExTree&lt;int&gt; treeOpt = build_opt(v);
          {
              //...
              treeOpt.evaluate();
          }
          {
              //...
              treeNai.evaluate();
          }
  }
  </code></pre>
  
  <p>I am asking this, because it seems to actually make a difference and i would like to know, why? Or, to ask more precisely, does the compiler realize, that treeOpt wont be used again after evaluate and frees to memory? The second code piece acutually causes <code>std::bad_alloc</code> to happen more often.</p>