About memory of a vector?

Discussion in 'C++' started by cylin, Jul 31, 2003.

  1. cylin

    cylin Guest

    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.
     
    cylin, Jul 31, 2003
    #1
    1. Advertising

  2. "cylin" <> wrote in message
    news:bga1l6$msp4d$-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
     
    John Harrison, Jul 31, 2003
    #2
    1. Advertising

  3. cylin

    cylin Guest

    "John Harrison" <> ¼¶¼g©ó¶l¥ó·s»D
    :bgac9b$miv1q$-berlin.de...
    >
    > "cylin" <> wrote in message
    > news:bga1l6$msp4d$-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
    >
    >
     
    cylin, Jul 31, 2003
    #3
  4. "cylin" <> wrote in message
    news:bgah0k$mq2k8$-berlin.de...
    >
    > "John Harrison" <> ¼¶¼g©ó¶l¥ó·s»D
    > :bgac9b$miv1q$-berlin.de...
    > >
    > > "cylin" <> wrote in message
    > > news:bga1l6$msp4d$-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
     
    John Harrison, Jul 31, 2003
    #4
  5. 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
     
    Kevin Goodsell, Jul 31, 2003
    #5
  6. cylin

    cylin Guest

    "John Harrison" <> ¼¶¼g©ó¶l¥ó·s»D
    :bgah7p$mncjg$-berlin.de...
    >
    > "cylin" <> wrote in message
    > news:bgah0k$mq2k8$-berlin.de...
    > >
    > > "John Harrison" <> ¼¶¼g©ó¶l¥ó·s»D
    > > :bgac9b$miv1q$-berlin.de...
    > > >
    > > > "cylin" <> wrote in message
    > > > news:bga1l6$msp4d$-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.
     
    cylin, Jul 31, 2003
    #6
  7. 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,
     
    Andrew Koenig, Jul 31, 2003
    #7
  8. "Andrew Koenig" <> wrote in message
    news:...
    > 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,


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

    john
     
    John Harrison, Jul 31, 2003
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pmatos
    Replies:
    6
    Views:
    23,962
  2. Replies:
    8
    Views:
    1,970
    Csaba
    Feb 18, 2006
  3. Javier
    Replies:
    2
    Views:
    593
    James Kanze
    Sep 4, 2007
  4. Rune Allnor
    Replies:
    4
    Views:
    965
    Rune Allnor
    Dec 11, 2008
  5. Rushikesh Joshi
    Replies:
    0
    Views:
    378
    Rushikesh Joshi
    Jul 10, 2004
Loading...

Share This Page