Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > About memory of a vector?

Reply
Thread Tools

About memory of a vector?

 
 
cylin
Guest
Posts: n/a
 
      07-31-2003
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.


 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      07-31-2003

"cylin" <(E-Mail Removed)> wrote in message
news:bga1l6$msp4d$(E-Mail Removed)-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


 
Reply With Quote
 
 
 
 
cylin
Guest
Posts: n/a
 
      07-31-2003

"John Harrison" <(E-Mail Removed)> 撰寫於郵件新聞
:bgac9b$miv1q$(E-Mail Removed)-berlin.de...
>
> "cylin" <(E-Mail Removed)> wrote in message
> news:bga1l6$msp4d$(E-Mail Removed)-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
>
>



 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      07-31-2003

"cylin" <(E-Mail Removed)> wrote in message
news:bgah0k$mq2k8$(E-Mail Removed)-berlin.de...
>
> "John Harrison" <(E-Mail Removed)> 撰寫於郵件新聞
> :bgac9b$miv1q$(E-Mail Removed)-berlin.de...
> >
> > "cylin" <(E-Mail Removed)> wrote in message
> > news:bga1l6$msp4d$(E-Mail Removed)-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


 
Reply With Quote
 
Kevin Goodsell
Guest
Posts: n/a
 
      07-31-2003
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

 
Reply With Quote
 
cylin
Guest
Posts: n/a
 
      07-31-2003

"John Harrison" <(E-Mail Removed)> 撰寫於郵件新聞
:bgah7p$mncjg$(E-Mail Removed)-berlin.de...
>
> "cylin" <(E-Mail Removed)> wrote in message
> news:bgah0k$mq2k8$(E-Mail Removed)-berlin.de...
> >
> > "John Harrison" <(E-Mail Removed)> 撰寫於郵件新聞
> > :bgac9b$miv1q$(E-Mail Removed)-berlin.de...
> > >
> > > "cylin" <(E-Mail Removed)> wrote in message
> > > news:bga1l6$msp4d$(E-Mail Removed)-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.



 
Reply With Quote
 
Andrew Koenig
Guest
Posts: n/a
 
      07-31-2003
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, http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      07-31-2003

"Andrew Koenig" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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, (E-Mail Removed)


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

john


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Patriot Memory PDC1G5600ELK Memory Review Silverstrand Front Page News 0 09-07-2005 02:24 AM
Differences between Sony Memory Stick & memory Stick Pro vs Memory Stick Duo? zxcvar Digital Photography 3 11-28-2004 10:48 PM
RAM Memory or virual memory Juli嫕 Sanz Garc燰 ASP .Net 4 11-12-2004 06:25 PM
GC does not release memory...memory keeps growing!!! Mahesh Prasad ASP .Net 1 02-22-2004 08:40 AM
AspNet Process Memory Issue on Win2k Server - Peformance is fine - Memory usuage doesn't stop growing Cy Huckaba ASP .Net 1 06-26-2003 04:00 AM



Advertisments