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 8 2894
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, 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 wrote: Victor,
vector<doubl e> 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
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(arbitrar y_small_number) ;
// Using it for huge settings
my_vec.reserve( huge_number);
for(int i = 0; i < huge_number; ++i)
my_vec.push_bac k(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_ve c);
// Now we need the vector object, but not its elements
{
// We create a tiny vector
std::vector<int > my_tmp_vec(arbi trary_small_num ber);
// 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.c om> wrote in message
news:11******** **************@ c13g2000cwb.goo glegroups.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
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" <tu****@gmail.c om> wrote in message
news:11******** *************@f 14g2000cwb.goog legroups.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 wrote: "Joseph Turian" <tu****@gmail.c om> wrote in message news:11******** *************@f 14g2000cwb.goog legroups.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 wrote: Howard wrote:
"Joseph Turian" <tu****@gmail.c om> wrote in message news:11****** *************** @f14g2000cwb.go oglegroups.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?). This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 for(int i = 0; i < 10; ++i) {
#5 *ii++ = 4;
#6 }
#7 copy(vi.begin(), vi.end(), ostream_iterator<int>(cout, " "));
|
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 greater than its size.
To use resize() function, it can't reduce capacity.
How to reduce capacity to zero or delete a vector type variable completely?...
|
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 I do
CDerived1* p_derived1 = new CDerived1();
base_class_vec.push_back(derived1);
|
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 "push_back" a copy of this into a
vector V. This is repeated many times in an iterative process.
Ok whenever I "push_back" a copy of Class A, I also want...
|
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;
| |
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 types or
std::vector<int>.
So where I would use an int* and reallocate it from time to time in C,
and randomly access it via , then I figure to...
|
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. As long as you
don't try to put in more objects than can be held in the initial block
of storage, everything proceeds rapidly. (If you do know how...
|
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 construction and destruction of a temporary,
and might still leave some excess capacity in the vector.
|
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 4.1.2-12)
The program below fails, but if the reserve(en)
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |