Go Back   Velocity Reviews > Newsgroups > C++
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

C++ - Shrink vector capacity?

 
Thread Tools Search this Thread
Old 01-19-2005, 08:47 PM   #1
Default Shrink vector capacity?


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



Joseph Turian
  Reply With Quote
Old 01-19-2005, 08:56 PM   #2
Victor Bazarov
 
Posts: n/a
Default Re: Shrink vector capacity?
Joseph Turian wrote:
> 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<double> v(1024*1024);

> 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


Victor Bazarov
  Reply With Quote
Old 01-19-2005, 09:05 PM   #3
Joseph Turian
 
Posts: n/a
Default Re: Shrink vector capacity?
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?

> 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



Joseph Turian
  Reply With Quote
Old 01-19-2005, 09:48 PM   #4
Victor Bazarov
 
Posts: n/a
Default Re: Shrink vector capacity?
Joseph Turian wrote:
> 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.

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

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

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

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


Good.

V


Victor Bazarov
  Reply With Quote
Old 01-20-2005, 12:41 AM   #5
Efrat Regev
 
Posts: n/a
Default Re: Shrink vector capacity?
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.



"Joseph Turian" <> wrote in message
news: oups.com...
> 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
>





Efrat Regev
  Reply With Quote
Old 01-20-2005, 08:31 PM   #6
Joseph Turian
 
Posts: n/a
Default Re: Shrink vector capacity?
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



Joseph Turian
  Reply With Quote
Old 01-20-2005, 10:06 PM   #7
Howard
 
Posts: n/a
Default Re: Shrink vector capacity?

"Joseph Turian" <> wrote in message
news: ups.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?
>
> Joseph
>


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

-Howard





Howard
  Reply With Quote
Old 01-27-2005, 01:18 PM   #8
Attila Feher
 
Posts: n/a
Default Re: Shrink vector capacity?
Howard wrote:
> "Joseph Turian" <> wrote in message
> news: ups.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




Attila Feher
  Reply With Quote
Old 01-27-2005, 03:22 PM   #9
Victor Bazarov
 
Posts: n/a
Default Re: Shrink vector capacity?
Attila Feher wrote:
> Howard wrote:
>
>>"Joseph Turian" <> wrote in message
>>news: roups.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?).


Victor Bazarov
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Why can't DVD Shrink locate the main movie Title 1 Pamela Farrington DVD Video 7 01-16-2006 05:35 PM
DVD Shrink ripping VOBs with no audio; solution? Traveller DVD Video 7 06-04-2005 05:54 PM
Re: DVD Shrink causing jerky video in PowerDVD? BigJIm DVD Video 7 04-02-2005 08:49 PM
DVD Shrink causing jerky video in PowerDVD? J Belly DVD Video 0 04-01-2005 11:06 AM
DVD Shrink: After Shrink to Hard drive, what's NEXT? hiddenat@lycos.com DVD Video 11 09-21-2004 02:41 AM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46