PDA

View Full Version : QVector::reserve() bug?



Caius Aérobus
3rd March 2008, 16:29
What is wrong in my code?


#include <QVector>

int main()
{
QVector<int> v;
printf("v.size()=%u\n", v.size());
v.reserve(2);
printf("v.size()=%u\n", v.size());
}

Result:


v.size()=0
v.size()=0

I expected that reserve() does allocate the required memory, as resize() does, so how does reserve() work actually?

jpn
3rd March 2008, 17:02
QVector::reserve() doesn't actually resize the vector but attempts to allocate memory for given amount of items. This ensures that the underlying data structure has large enough continuous memory block to enlarge up till given amount of items without need of moving data.

makarand.raut
5th June 2019, 17:35
You need to check the capacity of the vector. The size will change once you add the elements into it.

^NyAw^
6th June 2019, 11:34
Wow, only after 11 years you found a solution.

Please, look the posts date before answering anything.