[Development] QVector: clear while keeping capacity
Pocheptsov Timur
timur.pocheptsov at theqtcompany.com
Wed Nov 5 16:30:56 CET 2014
v.erase(v.begin(), v.end()) ?
________________________________________
From: development-bounces+timur.pocheptsov=theqtcompany.com at qt-project.org <development-bounces+timur.pocheptsov=theqtcompany.com at qt-project.org> on behalf of Smith Martin <Martin.Smith at theqtcompany.com>
Sent: Wednesday, November 5, 2014 3:36 PM
To: Milian Wolff; Allan Sandfeld Jensen
Cc: development at qt-project.org
Subject: Re: [Development] QVector: clear while keeping capacity
I think there should be a way to empty the vector without losing the memory. I would have said clear() should do that, but clear() says it releases the memory.
martin
________________________________________
From: development-bounces+martin.smith=theqtcompany.com at qt-project.org <development-bounces+martin.smith=theqtcompany.com at qt-project.org> on behalf of Milian Wolff <milian.wolff at kdab.com>
Sent: Wednesday, November 5, 2014 3:21 PM
To: Allan Sandfeld Jensen
Cc: development at qt-project.org
Subject: Re: [Development] QVector: clear while keeping capacity
On Wednesday 05 November 2014 14:33:00 Allan Sandfeld Jensen wrote:
> On Wednesday 05 November 2014, Milian Wolff wrote:
> > Hello all,
> >
> > could it be that QVector changed its behavior in Qt5? I just noticed that
> > resize(0) does deallocate memory, which is highly unexpected from my side.
> > See e.g. this old thread where it was said to use resize(0) to clear a
> > vector while keeping its capacity:
> >
> > http://comments.gmane.org/gmane.comp.lib.qt.general/42277,using
> >
> > But now in Qt5, I see in QVector::reallocData
> >
> > if (aalloc != 0) {
> >
> > ...
> >
> > } else {
> >
> > x = Data::sharedNull();
> >
> > }
> >
> > which triggers the deallocation of the memory. This can be seen by this
> > example program:
> >
> > #include <QVector>
> > #include <QDebug>
> >
> > int main()
> > {
> >
> > QVector<int> v;
> > v.fill(1, 100);
> > qDebug() << v.size() << v.capacity();
> > v.resize(0);
> > qDebug() << v.size() << v.capacity();
> > v.fill(1, 100);
> > qDebug() << v.size() << v.capacity();
> > v.clear();
> > qDebug() << v.size() << v.capacity();
> > return 0;
> >
> > }
> >
> > The output is for me:
> > 100 122
> > 0 0
> > 100 122
> > 0 0
> >
> > Is this truly desired behavior? It can trigger serious performance
> > issues...
>
> You need to use QVector::reserve() to reserve space. If you do, then resize
> should not auto shrink. I think that is fair behaviour. Anyone who knows how
> big the vector should be, should use reserve.
Ugh, that means when one does not know the capacity in advance, but doesn't
want to free either, one has to call v.reserve(v.capacity()) before calling
resize(0). Is this really desired? Ugh... But at least I now have a workaround
- thanks!
Bye
--
Milian Wolff | milian.wolff at kdab.com | Software Engineer
KDAB (Deutschland) GmbH&Co KG, a KDAB Group company
Tel. Germany +49-30-521325470, Sweden (HQ) +46-563-540090
KDAB - Qt Experts - Platform-independent software solutions
_______________________________________________
Development mailing list
Development at qt-project.org
http://lists.qt-project.org/mailman/listinfo/development
_______________________________________________
Development mailing list
Development at qt-project.org
http://lists.qt-project.org/mailman/listinfo/development
More information about the Development
mailing list