By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,159 Members | 979 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

About memory of a vector?

P: n/a
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.
Jul 19 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a

"cylin" <cy***@avant.com.tw> wrote in message
news:bg************@ID-154203.news.uni-berlin.de...
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
Jul 19 '05 #2

P: n/a

"John Harrison" <jo*************@hotmail.com> 撰寫於郵件新聞
:bg************@ID-196037.news.uni-berlin.de...

"cylin" <cy***@avant.com.tw> wrote in message
news:bg************@ID-154203.news.uni-berlin.de...
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>());


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

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

Regards,
cylin.


john

Jul 19 '05 #3

P: n/a

"cylin" <cy***@avant.com.tw> wrote in message
news:bg************@ID-154203.news.uni-berlin.de...

"John Harrison" <jo*************@hotmail.com> 撰寫於郵件新聞
:bg************@ID-196037.news.uni-berlin.de...

"cylin" <cy***@avant.com.tw> wrote in message
news:bg************@ID-154203.news.uni-berlin.de...
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>());


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
Jul 19 '05 #4

P: n/a
John Harrison wrote:

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

Jul 19 '05 #5

P: n/a

"John Harrison" <jo*************@hotmail.com> 撰寫於郵件新聞
:bg************@ID-196037.news.uni-berlin.de...

"cylin" <cy***@avant.com.tw> wrote in message
news:bg************@ID-154203.news.uni-berlin.de...

"John Harrison" <jo*************@hotmail.com> 撰寫於郵件新聞
:bg************@ID-196037.news.uni-berlin.de...

"cylin" <cy***@avant.com.tw> wrote in message
news:bg************@ID-154203.news.uni-berlin.de...
> 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>());
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

I See. Thanks,john.

Regards,
cylin.

Jul 19 '05 #6

P: n/a
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.

--
Andrew Koenig, ar*@acm.org
Jul 19 '05 #7

P: n/a

"Andrew Koenig" <ar*@acm.org> wrote in message
news:yu**************@tinker.research.att.com...
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.

--
Andrew Koenig, ar*@acm.org


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

john
Jul 19 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.