C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Shrink vector capacity?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Joseph Turian
Guest





PostPosted: Wed Jan 19, 2005 8:47 pm    Post subject: Shrink vector capacity? Reply with quote



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 cerr << v.capacity() << "n"; // outputs 1048576


Joseph

Back to top
Victor Bazarov
Guest





PostPosted: Wed Jan 19, 2005 8:56 pm    Post subject: Re: Shrink vector capacity? Reply with quote



Joseph Turian wrote:
Quote:
Is it possible to shrink the capacity of a vector?

No guaranteed way.

Quote:
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.

Quote:
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<double> v(1024*1024);

Quote:
cerr << v.capacity() << "n"; // outputs 1048576
v.clear();

'clear' does not change capacity. It changes 'size'.

Quote:
cerr << v.capacity() << "n"; // outputs 1048576
v = vector

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.

Quote:
cerr << v.capacity() << "n"; // outputs 1048576

Try

vector
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

Back to top
Joseph Turian
Guest





PostPosted: Wed Jan 19, 2005 9:05 pm    Post subject: Re: Shrink vector capacity? Reply with quote



Victor,

Quote:
vector<double> v = vector<double>(1024*1024);
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?

Quote:
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?

Quote:
Try vector<double>().swap(v);
FYI---at least on g++---this does in fact resize the capacity to 0.

Joseph


Back to top
Victor Bazarov
Guest





PostPosted: Wed Jan 19, 2005 9:48 pm    Post subject: Re: Shrink vector capacity? Reply with quote

Joseph Turian wrote:
Quote:
Victor,


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

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?

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.

Quote:
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?

None whatsoever, except that when there is no more memory left, it will
let you know.

Quote:
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.

Quote:
Try vector<double>().swap(v);

FYI---at least on g++---this does in fact resize the capacity to 0.

Good.

V

Back to top
Efrat Regev
Guest





PostPosted: Thu Jan 20, 2005 12:41 am    Post subject: Re: Shrink vector capacity? Reply with quote

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
// 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.



"Joseph Turian" <turian (AT) gmail (DOT) com> wrote

Quote:
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 cerr << v.capacity() << "n"; // outputs 1048576


Joseph




Back to top
Joseph Turian
Guest





PostPosted: Thu Jan 20, 2005 8:31 pm    Post subject: Re: Shrink vector capacity? Reply with quote

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

Back to top
Howard
Guest





PostPosted: Thu Jan 20, 2005 10:06 pm    Post subject: Re: Shrink vector capacity? Reply with quote


"Joseph Turian" <turian (AT) gmail (DOT) com> wrote

Quote:
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




Back to top
Attila Feher
Guest





PostPosted: Thu Jan 27, 2005 1:18 pm    Post subject: Re: Shrink vector capacity? Reply with quote

Howard wrote:
Quote:
"Joseph Turian" <turian (AT) gmail (DOT) com> wrote in message
news:1106253093.897922.78870 (AT) f14g2000cwb (DOT) googlegroups.com...
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?

"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.

--
Attila aka WW



Back to top
Victor Bazarov
Guest





PostPosted: Thu Jan 27, 2005 3:22 pm    Post subject: Re: Shrink vector capacity? Reply with quote

Attila Feher wrote:
Quote:
Howard wrote:

"Joseph Turian" <turian (AT) gmail (DOT) com> wrote in message
news:1106253093.897922.78870 (AT) f14g2000cwb (DOT) googlegroups.com...

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?

"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.

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?).

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.