^^^ pre-allocates a matrix of 100,100.
Main Topics
Browse All TopicsLet's say I've got a vector of vectors....
vector<vector<int> > myVector;
If I know the size of the outer and inner vectors, what is the most efficient way to pre-allocate ?
I'm also assuming the pre-allocation should be done outside any loops ?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
>> Is there a recommended limit to pre-allocation ?
http://www.cplusplus.com/r
That and the current memory usage of your application (lots of memory already allocated, highly fragmented, etc.)
>> As a test, a dynamically allocated array of 10 millon by 7 works fine ..... but myVector(10000000,vector<in
Note that the main difference here is that you (probably) tried allocating all memory in one block, while the vector doesn't necessarily do that.
Note that 7*10000000*sizeof(int) is already around 280MB on a 32bit system. That's a lot of memory.
>> is the performance saving on pre-allocating such a large VoV
If you don't reserve memory in a vector if has to allocate as it grows and this means it has to keep re-copying the internal buffer tot he new one. This can be VERY slow. If you can pre-allocate it is normally worth doing so but you do have to be mindful of how much memory you are useful... obviously :)
Have increased the points since this is obviously a bit more complicated than I first thought.... ;-)
Alright, so assuming I've got access to enough memory and the feng shui in my code is balanced etc. etc., how do I go from .....
vector<vector<int> > myVector(100, vector<int>(100));
to this mysterious multi-block computer friendly way of doing things ? I wouldn't know where to start !
I'll then leave you in peace (with a few extra points under your belt ! ) ;-)
Actually, before I answer that, maybe i'm just doing something stupid in my code.
std::cerr << "Vector Size: " << myVector.size() << " Vector Row Size: " << myVector[0].size() << endl;
With dynamic allocation of whatever value, I get the expected result :
Vector Size: X Vector Row Size: Y
However, once I introduce preallocation, I suddenly get double the values, 2X and 2Y !
Perhaps that's why things are crashing, the computer is allocating a massive amount of RAM !
My code is fairly straightforward though, so I don't know where I'm going wrong !
>> Oh just read it again... um, no that's not right!
Yeah, my mistake !
However your solution above doesn't seem to be working !
Before pre-allocaton, this worked fine.....
myVector[myRow].push_back(
However, with pre-allocation, this does not ...
myVector[myRow][0] = tempData[0];
cout << tempData[0] << "/" << myVector[myRow][0] << endl;
always outputs myVector[myRow][0] as zero.
Business Accounts
Answer for Membership
by: evilrixPosted on 2009-10-22 at 06:22:45ID: 25633814
Try this...
vector<vector<int> > myVector(100, vector<int>(100));