title: C++: vector of pointer loses the reference after push_back()
body: <p>In my code a have a global vector of Node object and a local vector of Node pointers:</p>
  
  <pre><code>#include&lt;cstdio&gt;
  #include&lt;cstdlib&gt;
  #include&lt;vector&gt;
  
  using namespace std;
  
  class Node {
      int n;
  
  public:
      Node(int i) : n(i);
      int getN() { return n; }
  };
  
  vector&lt;Node&gt; v;
  
  int main() {
      vector&lt;Node*&gt; p;
      v.push_back(Node(1));
      p.push_back(&amp;v[0]);
      printf("first node id : %d\n", (*p[0]).getN());
  
      return 0;
  }
  </code></pre>
  
  <p>I inserted a node object to global vector &amp; inserted the pointer of that object in the local vector. Output of my above code is:</p>
  
  <pre><code>first node id : 1
  </code></pre>
  
  <p>However, if I change my main function to this:</p>
  
  <pre><code>int main()
  {
      vector&lt;Node*&gt; p;
      v.push_back(Node(1));
      p.push_back(&amp;v[0]);
      v.push_back(Node(2));
      p.push_back(&amp;v[1]);
      printf("first node id : %d\n", (*p[0]).getN());
  
      return 0;
  }
  </code></pre>
  
  <p>The code prints a garbage value:</p>
  
  <pre><code>first node id : 32390176
  </code></pre>
  
  <p>I can't figure out the problem.
  Does the <code>vector</code> data structure changes the references of each object after an insertion ?
  How can I fix this ?</p>