View Full Version : vector memory allocation

23rd March 2006, 14:47
Hi all,
I know this is all very compiler/machine specific but...

how much space will
std::vector<int> aVector;
assign usually?

will my first aVector.push_back(n) have to assign memory before doing the push?

when aVector.push_back(n) is called repeatedly will it double the memory for the vector when needed or will it add blocks of some size?

std::vector<int> aVector;
The reserve() might not be necessary and could even be counter-productive if the definition assigns a block of 1024 anyway(?)
I'm searching an array for some condition that could appear once or 100000 times or not at all i.e I have no idea how big my vector will get but I don't want to assign 100000 places from the start. The process should be as fast as possible. Is it best to leave memory mangament to the machine, or should try to use reserve() cleverly?


23rd March 2006, 17:27
Which is more time critical ... adding elements or searching them? Will you be inserting/prepending or just appending?

I generally just let the std::vector handle allocation for me unless I have specific knowledge about how many elements I need.

You might consider the std::deque. It's usually better at managing memory ... especially when dealing with unknown growth since it allocates in blocks rather than total reallocation.