About memory of a vector?

C

cylin

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?
Thanks for your answer.

Regards,
cylin.
 
J

John Harrison

cylin said:
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?
Yes

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?
Thanks for your answer.

There's a trick.

vector<int> x;
....
x.swap(vector<int>());

The default constructed vector will have zero capacity, so swapping that
with vector x will give vector x zero capacity.
Regards,
cylin.

john
 
C

cylin

John Harrison said:
There's a trick.

vector<int> x;
...
x.swap(vector<int>());

If I do this, the total momery will release the part of x?
Or the total memory is still the same?
Thanks
 
J

John Harrison

cylin said:
If I do this, the total momery will release the part of x?
Or the total memory is still the same?
Thanks

This will release the memory of x. After the swap the memory of x will be
contained in the temporary object that is created with vector<int>(). The
destructor for that temporary object will release the memory that was
formerly in x. The destructor for the temporary object is called before the
next statement executes.

john
 
K

Kevin Goodsell

John said:
This will release the memory of x. After the swap the memory of x will be
contained in the temporary object that is created with vector<int>(). The
destructor for that temporary object will release the memory that was
formerly in x. The destructor for the temporary object is called before the
next statement executes.

john

For some reason my reply doesn't seem to have shown up yet, but I
mentioned in that reply that a vector may have a non-zero minimum
capacity, therefore the swap-with-a-temporary idiom doesn't necessarily
do exactly what the OP asked (make the capacity 0).

-Kevin
 
C

cylin

John Harrison said:
This will release the memory of x. After the swap the memory of x will be
contained in the temporary object that is created with vector<int>(). The
destructor for that temporary object will release the memory that was
formerly in x. The destructor for the temporary object is called before the
next statement executes.

john
I See. Thanks,john.

Regards,
cylin.
 
A

Andrew Koenig

John> There's a trick.

John> vector<int> x;
John> ...
John> x.swap(vector<int>());

Not quite. The trouble is that vector<int>() is an rvalue, and
the argument to swap requires an lvalue.

So you have to write it this way:

vector<int>().swap(x);

which relies on the fact that it is permissible to call the swap
member of an rvalue.
 
J

John Harrison

Andrew Koenig said:
John> There's a trick.

John> vector<int> x;
John> ...
John> x.swap(vector<int>());

Not quite. The trouble is that vector<int>() is an rvalue, and
the argument to swap requires an lvalue.

So you have to write it this way:

vector<int>().swap(x);

which relies on the fact that it is permissible to call the swap
member of an rvalue.

OK, thanks. Obviously one of the things my compiler doesn't complain about.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top