473,388 Members | 1,352 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

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

Jul 22 '05 #1
8 2873
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
Jul 22 '05 #2
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

Jul 22 '05 #3
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
Jul 22 '05 #4
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" <tu****@gmail.com> wrote in message
news:11**********************@c13g2000cwb.googlegr 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

Jul 22 '05 #5
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

Jul 22 '05 #6

"Joseph Turian" <tu****@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro 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

Jul 22 '05 #7
Howard wrote:
"Joseph Turian" <tu****@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro 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
Jul 23 '05 #8
Attila Feher wrote:
Howard wrote:
"Joseph Turian" <tu****@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googleg 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?).
Jul 23 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: john smith | last post by:
HI, when I try the following code, I get a segfault when compiled with VC.NET and g++ under cygwin. #1 vector<int> vi; #2 vector<int>::iterator ii = vi.begin(); #3 vi.reserve(10); #4 ...
7
by: cylin | last post by:
Dear all, We know that a vector can increase its capacity. Does it mean that system will allocate more memory to fit the value of capacity? If yes, then we maybe cost memory if capacity is...
11
by: ma740988 | last post by:
Consider a class - lets call it CObjectHolderClass that has a collection of object pointers like std::vector<CBaseClass*> base_class_vec; as member var. Now somewhere in my CObjectHolderClass...
34
by: Adam Hartshorne | last post by:
Hi All, I have the following problem, and I would be extremely grateful if somebody would be kind enough to suggest an efficient solution to it. I create an instance of a Class A, and...
10
by: Bob | last post by:
Here's what I have: void miniVector<T>::insertOrder(miniVector<T>& v,const T& item) { int i, j; T target; vSize += 1; T newVector; newVector=new T;
8
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value...
8
by: ma740988 | last post by:
Consider this statement in Excel's text Thinking in C++, Vol 2: /// 1 " A vector starts by grabbing a block of storage, as if it's taking a guess at how many objects you plan to put into it. ...
3
by: leeps_my | last post by:
I'm thinking of using "resize-to-size" to do the trimming: aVector.resize( aVector.size( ) ); I'm wondering why Scott Meyers was recommending "swap trick" instead, since the trick involves...
23
by: Mike -- Email Ignored | last post by:
In std::vector, is reserve or resize required? On: Linux mbrc32 2.6.22.1-41.fc7 #1 SMP Fri Jul 27 18:10:34 EDT 2007 i686 athlon i386 GNU/Linux Using: g++ (GCC) 4.1.2 20070502 (Red Hat...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.