[Development] QVector: clear while keeping capacity
Milian Wolff
milian.wolff at kdab.com
Wed Nov 5 14:19:00 CET 2014
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...
--
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
More information about the Development
mailing list