title: Size of vector of vector c++
body: <p>I'm trying to estimate the size in memory of a vector of a vector, but it seems I don't get the correct approximation.</p>
  
  <p>Here is the small code I wrote to check :</p>
  
  <pre><code>#include &lt;vector&gt;
  #include &lt;iostream&gt;
  using namespace std;
  
  int main(int argc, char** argv)
  {
          size_t n = 100;
          size_t m = 1000000;
  
          float sizeInKB = (sizeof(vector&lt;vector&lt;int&gt; &gt;) + n*sizeof(vector&lt;int&gt;) + n*m*sizeof(int))/1024.0f;
          cout &lt;&lt; sizeInKB &lt;&lt; " KB" &lt;&lt; endl;
  
          vector&lt;vector&lt;int&gt; &gt; vect(n);
  
          for(int i = 0; i &lt; n; ++i)
          {
                  vect[i].resize(m);
          }
  
          while(true)
          {}
  
          return EXIT_SUCCESS;
  }
  </code></pre>
  
  <p>As output, I get 390 630 KB, whereas the application takes 394 588 KB in memory according to the task manager. I agree that this is not the best way to know how much memory is used by the application (and especially by the vector) but it gives a good hint, and 4 MB is not only a few KB.</p>
  
  <p>Now if I try to estimate the size in memory of a vector of vector of vector, it gets more and more messy. With the same code, replacing <code>int</code> by <code>vector&lt;int&gt;</code> :</p>
  
  <pre><code>#include &lt;vector&gt;
  #include &lt;iostream&gt;
  using namespace std;
  
  int main(int argc, char** argv)
  {
          size_t n = 100;
          size_t m = 1000000;
  
          float sizeInKB = (sizeof(vector&lt;vector&lt;vector&lt;int&gt; &gt; &gt;) + n*sizeof(vector&lt;vector&lt;int&gt; &gt;) + n*m*sizeof(vector&lt;int&gt;))/1024.0f;
          cout &lt;&lt; sizeInKB &lt;&lt; " KB" &lt;&lt; endl;
  
          vector&lt;vector&lt;vector&lt;int&gt; &gt; &gt; vect(n);
  
          for(int i = 0; i &lt; n; ++i)
          {
                  vect[i].resize(m);
          }
  
          while(true)
          {}
  
          return EXIT_SUCCESS;
  }
  </code></pre>
  
  <p>As output I get 4 687 500 KB, whereas the application takes 6 271 028 KB in memory according to the task manager. There is a difference of more than 1.5 GB ... Where is this overhead coming from ? Is there a way to compute it ?</p>
  
  <p>I'm running all of this on Windows 7 Pro 64 bits, with Visual Studio 2008 ...</p>
  
  <p>Thanks in advance,</p>