I am trying to estimate the size in memory of a vector of a vector, but it seems that I am not getting the correct approximation.
Here is a little code that I wrote for verification:
#include <vector> #include <iostream> using namespace std; int main(int argc, char** argv) { size_t n = 100; size_t m = 1000000; float sizeInKB = (sizeof(vector<vector<int> >) + n*sizeof(vector<int>) + n*m*sizeof(int))/1024.0f; cout << sizeInKB << " KB" << endl; vector<vector<int> > vect(n); for(int i = 0; i < n; ++i) { vect[i].resize(m); } while(true) {} return EXIT_SUCCESS; }
As an output, I get 390 630 KB, while the application takes 394 588 KB in memory in accordance with the task manager. I agree that this is not the best way to find out how much memory is used by the application (and especially the vector), but it gives a good hint, and 4 MB is not only a few KB.
Now, if I try to estimate the size in memory of a vector of a vector of a vector, it becomes more and more dirty. With the same code, replacing int with vector<int> :
#include <vector> #include <iostream> using namespace std; int main(int argc, char** argv) { size_t n = 100; size_t m = 1000000; float sizeInKB = (sizeof(vector<vector<vector<int> > >) + n*sizeof(vector<vector<int> >) + n*m*sizeof(vector<int>))/1024.0f; cout << sizeInKB << " KB" << endl; vector<vector<vector<int> > > vect(n); for(int i = 0; i < n; ++i) { vect[i].resize(m); } while(true) {} return EXIT_SUCCESS; }
As an output, I get 4,687,500 KB, while the application takes 6,271,028 KB in memory in accordance with the task manager. There is a difference of more than 1.5 GB ... Where did this overhead come from? Is there any way to calculate it?
I run it all on 64-bit Windows 7 Pro, with Visual Studio 2008 ...
Thanks in advance,