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

Nikos Chantziaras realnc at arcor.de
Fri Jun 17 18:39:28 CEST 2011


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.



More information about the Qt-interest-old mailing list