title: How vector allocate memory
body: <p>Hii
    I know that vector allocated contiguous  fixed memory when it needs to push_back some items.And if its not able to fit into it it will alllocate new memory and copy old values into it and delete the old memory.
  If that is the case how the following code works </p>
  
  <pre><code>#include&lt;iostream&gt;  
  #include&lt;vector&gt;  
  using namespace std;   
  
  class Test   
  {
  public :   
      int val;   
      Test()   
      {
          val=0;   
      }   
  };   
  
  
  int main()   
  {
      vector&lt;Test&gt;vec;   
      Test t[10];   
      for (int i=0;i&lt;5;i++)   
          vec.push_back(t[i]);   
      cout&lt;&lt;vec.capacity()&lt;&lt;endl; //printing 8   
      Test* obj=&amp;vec[2];   
      obj-&gt;val=2;   
      cout&lt;&lt;obj&lt;&lt;endl;   
      for (int i=0;i&lt;5;i++) //Adding 5 elements more 
          vec.push_back(t[i]);   
      Test* obk=&amp;vec[2];    
      cout&lt;&lt;obk-&gt;val&lt;&lt;endl;    
      cout&lt;&lt;obj-&gt;val&lt;&lt;endl;    
      cout&lt;&lt;obj&lt;&lt;endl;  
      cout&lt;&lt;obk&lt;&lt;endl;  
  }  
  </code></pre>
  
  <p>Here if you see obj is taking pointer of vec[2] which value in my machine is coming 0x8bcb048 .Then I am inserting 5 more items so vector will allocate new memory.Now if i am taking obk from vector[2] its coming different address 0x8bcb070 . But if i try to acess i with the help of obj its not giving any problem.Any reason ?</p>