![]() |
|
|
|
#1 |
|
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 |
|
|
|
|
#2 |
|
Posts: n/a
|
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 |
|
|
|
#3 |
|
Posts: n/a
|
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 |
|
|
|
#4 |
|
Posts: n/a
|
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 |
|
|
|
#5 |
|
Posts: n/a
|
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 |
|
|
|
#6 |
|
Posts: n/a
|
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 |
|
|
|
#7 |
|
Posts: n/a
|
"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 |
|
|
|
#8 |
|
Posts: n/a
|
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 |
|
|
|
#9 |
|
Posts: n/a
|
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 |
|
![]() |
| Thread Tools | Search this Thread |
|
|
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 |