summaryrefslogtreecommitdiffstats
path: root/src/corelib/tools/qvector.cpp
diff options
context:
space:
mode:
authorMarc Mutz <marc.mutz@kdab.com>2016-02-16 15:24:38 +0100
committerMarc Mutz <marc.mutz@kdab.com>2016-02-29 07:57:47 +0000
commita7885c9756d423042bd0670d82d78d8dffe9be54 (patch)
tree913f0db5b4ef655c6d708781e7de5a352e5c5043 /src/corelib/tools/qvector.cpp
parent85c2a128ef8d1b5fbddf551691bccc0770e558ba (diff)
QVector: preserve capacity in clear()
This is what std::vector implementations usually do, because it minimizes memory fragmentation and useless allocations since no user will call clear() unless she intends to append new data afterwards. Fix calls to resize(0) that show how existing code tried to work around the issue. Adjust test. Port from QVERIFY(==) to QCOMPARE as a drive-by. [ChangeLog][QtCore][QVector] clear() now preserves capacity. To shed capacity, call squeeze() or swap with a default-constructed QVector object, see the documentation for an example. Change-Id: I9cebe611a97e027a89e821e64408a4741b31f1f6 Reviewed-by: Lars Knoll <lars.knoll@theqtcompany.com>
Diffstat (limited to 'src/corelib/tools/qvector.cpp')
-rw-r--r--src/corelib/tools/qvector.cpp15
1 files changed, 13 insertions, 2 deletions
diff --git a/src/corelib/tools/qvector.cpp b/src/corelib/tools/qvector.cpp
index b3247b8af5..ef1c9c17b0 100644
--- a/src/corelib/tools/qvector.cpp
+++ b/src/corelib/tools/qvector.cpp
@@ -502,8 +502,19 @@
/*! \fn void QVector::clear()
- Removes all the elements from the vector and releases the memory used by
- the vector.
+ Removes all the elements from the vector.
+
+ \note Until Qt 5.6, this also released the memory used by
+ the vector. From Qt 5.7, the capacity is preserved. To shed
+ all capacity, swap with a default-constructed vector:
+ \code
+ QVector<T> v ...;
+ QVector<T>().swap(v);
+ Q_ASSERT(v.capacity() == 0);
+ \endcode
+ or call squeeze().
+
+ \sa squeeze()
*/
/*! \fn const T &QVector::at(int i) const