[Qt-interest] How to clear QVector without releasing memory?

Atlant Schmidt aschmidt at dekaresearch.com
Tue Jun 21 12:58:25 CEST 2011


Nikos:

> No, I did mean reserve().

  It's funny; I wrote a full reply to you discussing
  reserve() before I noticed the previous discussion
  had all been about resize() so I re-wrote the entire
  reply.

  But my argument was basically the same.

    void QVector::reserve ( int size )

    Attempts to allocate memory for at least size elements.
    If you know in advance how large the vector will be,
    you can call this function, and if you call resize()
    often you are likely to get better performance. If
    size is an underestimate, the worst that will happen
    is that the QVector will be a bit slower.

    The sole purpose of this function is to provide a
    means of fine tuning QVector's memory usage. In
    general, you will rarely ever need to call this
    function. If you want to change the size of the
    vector, call resize().

    See also squeeze() and capacity().


  Here, we have the disclaimer "The sole purpose of this
  function is to provide a means of fine tuning QVector's
  memory usage." This seems to specifically argue *AGAINST*
  having any expectations about what this will do to already-
  allocated elements in the QVector.

  I stand on the same position as yesterday and with the
  other poster: there's no documented way to do what he
  wants to do, sensible though his request may be.

                              Atlant

-----Original Message-----
From: qt-interest-bounces+aschmidt=dekaresearch.com at qt.nokia.com [mailto:qt-interest-bounces+aschmidt=dekaresearch.com at qt.nokia.com] On Behalf Of Nikos Chantziaras
Sent: Monday, June 20, 2011 21:45
To: qt-interest at qt.nokia.com
Subject: Re: [Qt-interest] How to clear QVector without releasing memory?

On 06/20/2011 06:16 PM, Atlant Schmidt wrote:
> Nikos:
>
>    (You switched from talking about resize() to reserve();
>    I'll assume that's a typo.)

No, I did mean reserve().  Because that's what you're using to guarantee
a certain vector capacity and thus memory allocation.


>
>    Here's the description for resize() (from the Qt 4.7.1 docs):
>
>      void QVector::resize ( int size )
>
>      Sets the size of the vector to size. If size is
>      greater than the current size, elements are added
>      to the end; the new elements are initialized with
>      a default-constructed value. If size is less than
>      the current size, elements are removed from the end.
>
>      See also size().
>
>
>    It seems to me that there's no promise that this clears
>    existing elements in the vector (except that elements
>    at the end will be destroyed if you're shortening the
>    vector). In fact, the behaviour when the new size ==
>    the existing size is completely unspecified!

What do you mean with "clears existing elements".  How can you "clear"
an element?  This is not an operation I'm familiar with.
_______________________________________________
Qt-interest mailing list
Qt-interest at qt.nokia.com
http://lists.qt.nokia.com/mailman/listinfo/qt-interest

This e-mail and the information, including any attachments, it contains are intended to be a confidential communication only to the person or entity to whom it is addressed and may contain information that is privileged. If the reader of this message is not the intended recipient, you are hereby notified that any dissemination, distribution or copying of this communication is strictly prohibited. If you have received this communication in error, please immediately notify the sender and destroy the original message.

Thank you.

Please consider the environment before printing this email.



More information about the Qt-interest-old mailing list