title: Why my pointers get invalidated, if I don't use vector.reserve()?
body: <p>I have small piece of code, which containts a vector of class(Vector2) for 2-dimensional vertices and a vector of template class DynamicLine which takes Vector2 type object for argument.</p>
  
  <pre><code>std::vector&lt;Vector2&gt; m_coords;
  std::vector&lt;DynamicLine&lt;Vector2&gt;&gt; m_lines;
  Vector2 *lineVec = NULL;
  
  
  
  
  
   bool draw = true;
   case DrawingWidgetState::ADD_VERTEX_SELECTED:{
                  for(auto it = m_coords.begin(); it != m_coords.end(); it++)
                       if(it-&gt;distanceFrom(Vector2(event-&gt;x(), event-&gt;y())) &lt; 20 &amp;&amp; !m_coords.empty()){
                          draw = false;
                          break;
                       }
  
                  if(draw){
  
                      if(event-&gt;x() &lt; m_mainWindow-&gt;width() - 10 &amp;&amp; event-&gt;x() &gt;= 10
                              &amp;&amp; event-&gt;y() &lt; m_mainWindow-&gt;height() &amp;&amp; event-&gt;y() &gt;= 10) {
  
  
                          m_coords.push_back(Vector2(event-&gt;x(), event-&gt;y()));
                          update();
                      }
                  }
                  break;
  
              }
  
  
  case DrawingWidgetState::ADD_LINE_SELECTED:{
                  for(auto it = m_coords.begin(); it != m_coords.end(); it++)
                      if(it-&gt;distanceFrom(Vector2(event-&gt;x(), event-&gt;y())) &lt; 10){
  
                          if(!i){
                              lineVec = &amp;(*it);
                              i++;
                          }
  
                          else{
  
                              m_lines.push_back(DynamicLine&lt;Vector2&gt;(lineVec, &amp;(*it)));
                              i = 0;
  
                          }
                          update();
                          break;
                      }
                  break;
  
              }
  </code></pre>
  
  <p>Problem is next. For example if I add two vertices and a line between them then everything works fine. But if I now add another vertex(m_coords.push_back(Vector2(event->x(), event->y()));) then the line dissaperas and values in m_lines vector are changed to very random big numbers. I read something about pointers getting invalidated. If you push new values to vector then some will eventually get expanded. I also fixed my problem using vector::reserve, but since I'm sure I'm not doing this right could anyone please explain me how should I use vector::reserve and why I need to do that?</p>