Shrink vector capacity?

J

Joseph Turian

Fellow hackers,

Is it possible to shrink the capacity of a vector?

i.e. Without new'ing and delete'ing a vector, can one return its memory
to the heap?

Here's what I get under the g++ implementation:

vector<double> v = vector<double>(1024*1024);
cerr << v.capacity() << "\n"; // outputs 1048576
v.clear();
cerr << v.capacity() << "\n"; // outputs 1048576
v = vector<double>();
cerr << v.capacity() << "\n"; // outputs 1048576


Joseph
 
V

Victor Bazarov

Joseph said:
Is it possible to shrink the capacity of a vector?

No guaranteed way.
i.e. Without new'ing and delete'ing a vector, can one return its memory
to the heap?

Nothing in the language says that even delete'ing anything returns that
memory to the heap. There is no re-use guarantee for free store.
Here's what I get under the g++ implementation:

vector<double> v = vector<double>(1024*1024);

You're potentially wasting more memory than you need. Just do

vector said:
cerr << v.capacity() << "\n"; // outputs 1048576
v.clear();

'clear' does not change capacity. It changes 'size'.
cerr << v.capacity() << "\n"; // outputs 1048576
v = vector<double>();

Assignment does not necessarily change capacity. It only copies elements
and as a side effect, changes 'size'. Reallocation would happen only if
the existing 'v's capacity is less than needed.
cerr << v.capacity() << "\n"; // outputs 1048576

Try

vector<double>().swap(v);

Again, no guarantees.

The reason shrinking a vector is not an easy thing is simple: shrinking
would require reallocation, and reallocation is the most expensive
operation.

V
 
J

Joseph Turian

Victor,
You're potentially wasting more memory than you need. Just do
vector<double> v(1024*1024);

I don't understand. How is this potentially wasting more memory than
the original expression?
Nothing in the language says that even delete'ing anything returns that
memory to the heap. There is no re-use guarantee for free store.

What guarantee does it give?
If I new a 1M element vector, delete it, and new another 1M element
vector, am I at least guaranteed that the program needs only 1MB
instead of 2?
Try vector<double>().swap(v);
FYI---at least on g++---this does in fact resize the capacity to 0.
Joseph
 
V

Victor Bazarov

Joseph said:
Victor,




I don't understand. How is this potentially wasting more memory than
the original expression?

The original expression creates a temporary vector and then copy-
initialises the 'v' vector with it. The compiler is allowed to forgo
creation of the temporary (and most probably do), yet, according to
the rules of the language, a temporary _may_ be created.
What guarantee does it give?

None whatsoever, except that when there is no more memory left, it will
let you know.
If I new a 1M element vector, delete it, and new another 1M element
vector, am I at least guaranteed that the program needs only 1MB
instead of 2?

Nope. That's an issue between your implementation and your OS.
FYI---at least on g++---this does in fact resize the capacity to 0.

Good.

V
 
E

Efrat Regev

Hello,

Scott Meyer's explains in "Effective STL" how to use the
std::vector's swap() method to shrink vectors. Perhaps this is relevant for
you. It works as follows:

// Creating a vector
std::vector<int> my_vec(arbitrary_small_number);

// Using it for huge settings
my_vec.reserve(huge_number);
for(int i = 0; i < huge_number; ++i)
my_vec.push_back(i);

// (Above code can be done more efficiently using std::generate, but that's
irrelevant)

// Do something with the vector
my_do_something_function(my_vec);

// Now we need the vector object, but not its elements
{
// We create a tiny vector
std::vector<int> my_tmp_vec(arbitrary_small_number);

// We swap the vectors
my_vec.swap(my_tmp_vec);

// Implicitly, my_tmp_vec's destructor called. Presumably, the allocator
gains all of its memory.
}

// At this point my_vec is truly small (which is waht we wanted).

I hope this helps,

Ami

P.S. Scott Meyer writes excellent books; even if this example doesn't help
you, I highly recommend his writings.
 
J

Joseph Turian

Ami,

Thank you for the reply.
I will definitely check out his books at my library.
Any non-STL books of his that you particularly recommend?

Joseph
 
H

Howard

Joseph Turian said:
Ami,

Thank you for the reply.
I will definitely check out his books at my library.
Any non-STL books of his that you particularly recommend?

Joseph

"Effective C++, Second Edition." and "More Effective C++" are both
indispensible, as far as I'm concerned!

-Howard
 
A

Attila Feher

Howard said:
"Effective C++, Second Edition." and "More Effective C++" are both
indispensible, as far as I'm concerned!

They are also available on a tree-saving, cost effective CD-ROM edition.
Both books (and some more goodies) on one CD-ROM.
 
V

Victor Bazarov

Attila said:
They are also available on a tree-saving, cost effective CD-ROM edition.

I take it you're being sarcastic here.

The tree-saving properties of compact discs hasn't been researched, AFAIK.
As to cost effectiveness, how do you measure it? Yes, it costs about $1
to produce, so does a book nowadays. Besides, the sheer convenience of
the CD... You just need a computer and a power source to read it. You
can always take it on the road. It fits conveniently in a notebook case
and weighs only 7 pounds (what's the weight of an average notebook PC?).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top