Skip to main content
GameDev.net gamedev.net
🔒 Locked

STL Vector reserve() questions

Started by Hybrid Jan 29, 2004 at 9:56 AM 4 replies 3.4k views
Original Post
Hybrid
Hybrid
I haven''t been able find information on what happens *after* you do a reserve() on an STL vector. Say I have a vector and I do a reserve(100) on it. Now then... a) If I fill the vector full with 100 items and then delete 2 of them, will the vector still be 100 in ''size'' or will it now be 98. That is if I add those 2 elements back into the vector again, will the vector have to allocate memory for them. b) If I call clear() will the reserve(100) still be in effect? Basically, how ''permanent'' is the reserve() function. Are there commands that override it and mess around with the memory allocation - which reserve() is designed to get around. Thanks
Fruny
Fruny
* reserve(100) will allocate capacity for *at least* 100 elements.

a) size() is 98, capacity() is still (at least) 100.
b) That's implementation-defined. Usually, the capacity() stays put.


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” — Brian W. Kernighan (C programming language co-inventor)

[edited by - Fruny on January 29, 2004 11:15:19 AM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
sark
sark
It depends on the STL implementation, although most modern ones will answer:

a) Memory allocated will still be 100 * object size
b) Yes
TangentZ
TangentZ
reserve() changes the "capacity" of the vector, which is the
maximum number of elements before the vector has to expand.

a) no, because the vector has enough capacity (but the
implementation may choose to, so it depends).

b) yes, the capacity remains the same. The size will be 0 though.

The effect of reserve() remains until the next time you call
it, or when the vector needs to expand.




Kami no Itte ga ore ni zettai naru!
神はサイコロを振らない!
Qw3r7yU10p!
Qw3r7yU10p!
'capacity' tells you how much controlled memory there is
'size' tells you how many elements there are
so if you delete 2 the the size will be 98

It isn't possible to shrink the capacity directly.

what you can do though is copy it into a new vector and then swap the contents.

vector<double> values(100);
values.pop_back();
values.pop_back();//now only got 98 values

// but capacity is (probably) 100


vector<double> temp(values);//copy to a temporary vector, size 98

values.swap(temp);//swap internals back to original

// values size will be 98 and capacity (quite likely) 98


this is quite a common way of doing it

[edited by - petewood on January 29, 2004 11:41:19 AM]
Hybrid
Hybrid
Thanks for the answers everyone. Very helpful and exactly what I was after.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.