deallocating vector storage

Discussion in 'C++' started by Tino, Jul 9, 2004.

  1. Tino

    Tino Guest

    In TC++PL (third edition), Section 16.3.8, Stroustrup gives the
    following as a way to give memory from a std::vector back to the
    system:

    vector<int> tmp = v; // v.size() == 0
    v.swap( tmp ); // v now has capacity of tmp (default), extra capacity
    returned to system

    We need to have a general method that returns memory back to the
    system for vectors which are no longer needed, but have not yet gone
    out of scope. Are there alternative suggestions for accomplishing
    this?

    Regards,
    Ryan
    Tino, Jul 9, 2004
    #1
    1. Advertising

  2. Tino wrote:
    > In TC++PL (third edition), Section 16.3.8, Stroustrup gives the
    > following as a way to give memory from a std::vector back to the
    > system:
    >
    > vector<int> tmp = v; // v.size() == 0
    > v.swap( tmp ); // v now has capacity of tmp (default), extra capacity
    > returned to system
    >
    > We need to have a general method that returns memory back to the
    > system for vectors which are no longer needed, but have not yet gone
    > out of scope. Are there alternative suggestions for accomplishing
    > this?


    There is no guarantee that the storage will be returned. Swapping with
    an empty vector, clearing the vector, resizing it to 0, all are good
    attempts to accomplish what you need, but the Standard makes no promises
    WRT the memory allocation behaviour. It's all platform-dependent and
    implementation-dependent, I believe.

    V
    Victor Bazarov, Jul 9, 2004
    #2
    1. Advertising

  3. Tino wrote:

    > In TC++PL (third edition), Section 16.3.8, Stroustrup gives the
    > following as a way to give memory from a std::vector back to the
    > system:
    >
    > vector<int> tmp = v; // v.size() == 0
    > v.swap( tmp ); // v now has capacity of tmp (default), extra capacity
    > returned to system




    Why I do not see this in 16.3.8?



    The above has no real effect (it exchanges emptiness) and no memory is
    returned to the system.



    > We need to have a general method that returns memory back to the
    > system for vectors which are no longer needed, but have not yet gone
    > out of scope. Are there alternative suggestions for accomplishing
    > this?



    Yes. The erase(), clear(), resize(), pop_back() member functions.






    Regards,

    Ioannis Vranos
    Ioannis Vranos, Jul 9, 2004
    #3
  4. Ioannis Vranos wrote:

    >> We need to have a general method that returns memory back to the
    >> system for vectors which are no longer needed, but have not yet gone
    >> out of scope. Are there alternative suggestions for accomplishing
    >> this?

    >
    >
    >
    > Yes. The erase(), clear(), resize(), pop_back() member functions.




    And if you mean to get rid of the container itself, you can make it on
    the free store, however the whole thing sounds like a bad design. Why
    you would not need a vector till the end of the scope? Perhaps the scope
    is larger than it should.






    Regards,

    Ioannis Vranos
    Ioannis Vranos, Jul 9, 2004
    #4
    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. milkyway

    Deallocating storage

    milkyway, Feb 2, 2005, in forum: Java
    Replies:
    7
    Views:
    455
    John C. Bollinger
    Feb 3, 2005
  2. binaya

    deallocating memory twice

    binaya, Nov 6, 2003, in forum: C++
    Replies:
    2
    Views:
    362
    Ron Natalie
    Nov 6, 2003
  3. George Sakkis

    Fatal Python error: deallocating None

    George Sakkis, Oct 26, 2006, in forum: Python
    Replies:
    3
    Views:
    1,898
    apunga
    Jun 13, 2011
  4. sarathy
    Replies:
    2
    Views:
    650
    sarathy
    Jul 17, 2006
  5. Replies:
    8
    Views:
    1,892
    Csaba
    Feb 18, 2006
Loading...

Share This Page