title: How does std::vector::end() iterator work in memory?
body: <p>Today, I was attempting to extract a subset of N elements from a vector of size M, where N &lt; M. I realised that I did not need to create a new copy, only needed to modify the original, and moreover, could take simply the first N elements.</p>
  
  <p>After doing a few brief searches, there were many answers, the most attractive one being resize() which appears to truncate the vector down to length, and deal neatly with the memory issues of erasing the other elements.</p>
  
  <p>However, before I came across vector.resize(), I was trying to point the vector.end() to the N+1'th position. I knew this wouldn't work, but I wanted to try it regardless. This would leave the other elements past the N'th position "stranded", and I believe (correct me if i'm wrong) this would be an example of a memory leak.</p>
  
  <p>On looking at the iterator validity on <a href="http://www.cplusplus.com/reference/vector/vector/resize/" rel="nofollow noreferrer">http://www.cplusplus.com/reference/vector/vector/resize/</a>, 
  we see that if it shrinks, vector.end() stays the same. If it expands, vector.end() will move (albeit irrelevant to our case).</p>
  
  <p>This leads me to question, <strong>what is the underlying mechanic of vector.end()?</strong> Where does it lie in memory? It can be found incrementing an iterator pointing to the last element in the vector, eg auto iter = &amp;vector.back(), iter++, but in memory, is this what happens?</p>
  
  <p>I can believe that at all times, what follows vector.begin() should be the first element, but on resize, it appears that vector.end() can lie elsewhere other than past the last element in the vector.</p>
  
  <p>For some reason, I can't seem to find the answer, but it sounds like a very basic computer science course would contain this information. I suppose it is stl specific, as there are probably many implementations of a vector / list that all differ...</p>
  
  <p>Sorry for the long post about a simple question!</p>