title: How do I find the max size that can be allocated to a vector in runtime?
body: <p>I am testing to initialise a vector to the maximum possible size in runtime. Initially, I thought <code>(size_t)-1</code> gives me a theoretical maximum while <code>vector::max_size()</code> gives the true runtime maximum. After encountering application failure and asking this <a href="https://stackoverflow.com/questions/34221482/unknown-application-failure-during-vector-initialization">question</a>, I have realised that is also another theoretical limit. Further research turns up <a href="http://www.cplusplus.com/forum/general/73581/" rel="nofollow noreferrer">this</a>:</p>
  
  <blockquote>
    <p>(maximum size a vector can reach) limited by the largest contiguous chunk of RAM your OS can allocate for you or the return value of vector&lt;>::max_size(), whichever is smaller.</p>
  </blockquote>
  
  <p>In vectors (and probably containers in general), how do I find this true runtime maximum? I am currently still midway through building up my application (it is a practice project), so I will accept any reasonably elegant and efficient solution that can give an exact value or close but safe approximation.</p>