title: Overwriting Vectors C++
body: <p>I was trying to overwrite values into my vector:</p>
  
  <pre><code>  for (i = 0; i &lt; 50; i++)
    {      
          LJ.clear();
          LJ.reserve(210);
  
          //Calculate Lennard-Jones potential of every pair
          for(itr = 0; itr &lt; 210; itr++)
          {
              term1 = pow(r[itr], 12);
              term1 = 1/term1;
              term2 = pow(r[itr], 6);
              term2 = 2/term2;
              LJ.push_back(term1 - term2);
          }
  
          //Calculate the initial Energy in the system Ei
          Ei = accumulate(LJ.begin(), LJ.end(), 0.0);
          Ei = Ei/2;
          cout &lt;&lt; Ei &lt;&lt; endl;
      }
  </code></pre>
  
  <p>However, whenever I came across the loop twice, the capacity of the vector got some garbage value and when i tried to push an item in, I got a segmentation fault...and if i left our the reserve part, I still got an a seg fault</p>