title: vector.push_back() crashes
body: <p>I have a problem while working with vectors of a class. I have tried whittling away at the code as much as possible, leaving me with the code below. Yet, after doing this, I still cannot figure out why my program is crashing. I come from a world of VBA and my C++ debugging skills are poor to say the least and I apologize for that up front. Any guidance to improve my ability here will be gladly accepted.</p>
  
  <p>My class:  </p>
  
  <pre><code>class Tester {
  public:
  
      int varA;
      int varB;
  
      Tester() {
          varA = 1;
          varB = 1;
      }
  
      Tester(Tester&amp; P1, Tester&amp; P2) {
          varA = P1.varA + P2.varA;
          varB = P1.varB + P2.varB;
      }
  
      Tester(const Tester &amp;Source) {
          varA = Source.varA;
          varB = Source.varB;
      }
  
  };
  </code></pre>
  
  <p>My test mains:<br>
  <strong>Does not work:</strong></p>
  
  <pre><code>int main() {
  std::vector &lt; Tester &gt; BreakIt;
  
      for (int i = 0; i &lt; 2500; i++) {
          Tester newTester;
          BreakIt.push_back(newTester);
          Tester&amp; tempTester = BreakIt.at(0);
          for (int j = 0; j &lt; 4; j++) {
              BreakIt.push_back(Tester(newTester, tempTester)); //This is where it crashes.
          }
      }
  
  return 0;
  }
  </code></pre>
  
  <p><strong>Does work:</strong></p>
  
  <pre><code>int main() {
  std::vector &lt; Tester &gt; BreakIt;
  
      Tester newTester;
      BreakIt.push_back(newTester);
  
      for (int i = 0; i &lt; 2500; i++) {
          Tester&amp; tempTester = BreakIt.at(0);
          for (int j = 0; j &lt; 4; j++) {
              BreakIt.push_back(Tester(newTester, tempTester));
          }
      }
  
  return 0;
  }
  </code></pre>
  
  <p><strong>Does work:</strong></p>
  
  <pre><code>int main() {
  std::vector &lt; Tester &gt; BreakIt;
  
      for (int i = 0; i &lt; 2500; i++) {
          Tester newTester;
          BreakIt.push_back(newTester);
          Tester&amp; tempTester = BreakIt.at(0);
      }
  
  return 0;
  }
  </code></pre>
  
  <p>The <code>push_back()</code> line that breaks does run a few times before the crash. It seems that when I change certain things around and recompile, the point at which it crashes changes (i.e. 20 times through the main loop vs. 175 times vs. 1000 times). However, once I've compiled, it will typically run through the the same iteration each time before crashing.</p>
  
  <p>I suspect that this is some kind of 'undefined behavior' but from where I'm not sure.</p>
  
  <p>Can someone please help me identify why <code>BreakIt.push_back(Tester(newTester, tempTester));</code> does not work where I want it to?</p>