[Qt-interest] How to clear QVector without releasing memory?
Atlant Schmidt
aschmidt at dekaresearch.com
Mon Jun 20 17:16:31 CEST 2011
Nikos:
(You switched from talking about resize() to reserve();
I'll assume that's a typo.)
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!
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 09:54
To: qt-interest at qt.nokia.com
Subject: Re: [Qt-interest] How to clear QVector without releasing memory?
I think even though it's not explicitly spelled out in the docs, it's
implied. reserve() would have no meaning otherwise. What's the point
of offering this function if Qt would then go ahead and lower the
capacity again?
On 06/20/2011 04:49 PM, Schimkowitsch Robert wrote:
> resize(0) does really seem to be an option here. The way I understood
> the source code, it really will not free up memory when I resize the
> vector. What bothers me a little is that I would rely on undocumented
> behaviour here, i.e. it might change without notice.
>
> But the way I see it, there IS no documented way to remove items without
> deallocating memory.
>
> Robert Schimkowitsch
>
>
> -----Original Message-----
> From: qt-interest-bounces+robert.schimkowitsch=andritz.com at qt.nokia.com
> [mailto:qt-interest-bounces+robert.schimkowitsch=andritz.com at qt.nokia.co
> m] On Behalf Of Nikos Chantziaras
> Sent: Friday, June 17, 2011 6:39 PM
> To: qt-interest at qt.nokia.com
> Subject: Re: [Qt-interest] How to clear QVector without releasing
> memory?
>
> On 06/17/2011 05:19 PM, Schimkowitsch Robert wrote:
>> Hi,
>>
>> I have a QVector that I want to re-use many times as a buffer. As the
>> application is long-running on a low memory device, memory
> fragmentation
>> might be an issue.
>>
>> To keep this in check, I would like to reserve() the QVector once,
> with
>> the maximum size I am going to need, and then re-use it without ever
>> freeing up memory.
>>
>> The only ways to clear all items from the vector I found will also
>> deallocate memory.
>>
>> What is the performace-optimal way to clear a QVector without
>> deallocating memory?
>
> QVector::resize(0). It doesn't clear anything, which means it's as fast
>
> as it can possibly get. The docs are hinting that this does not
> deallocate memory:
>
> "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."
>
> Dot not use QVector::clear(). This one will deallocate memory,
> according the docs.
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at qt.nokia.com
> http://lists.qt.nokia.com/mailman/listinfo/qt-interest
>
>
> #####################################################################################
>
> This message and any attachments are solely for the use of the intended recipients. They may contain privileged and/or confidential information or other information protected from disclosure. If you are not an intended recipient, you are hereby notified that you received this email in error and that any review, dissemination, distribution or copying of this email and any attachment is strictly prohibited. If you have received this email in error, please contact the sender and delete the message and any attachment from your system.
>
> Thank You.
>
> #####################################################################################
_______________________________________________
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