user2610457
user2610457

Reputation: 21

Is there a maximum size for QVector?

I've tried to append 100 000 QString elements (each QString has about 10 characters in it) to a QVector. After that the program fails. Are there some limitations to how many elements a QVector can contain (besides physical memory limitations of course)? Besides, I think a have a lot free memory, enough to store such bunch of strings. What am I doing wrong?

Upvotes: 2

Views: 4103

Answers (3)

Tomasso
Tomasso

Reputation: 711

From the QVector documentation for Qt 5.14.2:

The current version of QVector is limited to just under 2 GB (2^31 bytes) in size.

Upvotes: 0

UmNyobe
UmNyobe

Reputation: 22890

From the documentation.

The QVector class is a template class that provides a dynamic array... It stores its items in adjacent memory locations and provides fast index-based access.

Knowing this the best way to append a large number of elements is to reserve to memory either using

QVector<QString> vector(100000);//or 
vector.reserve(100000);

This avoids relocating several times the memory.

Upvotes: 2

Sebastian Lange
Sebastian Lange

Reputation: 4029

Try using QStringList as suggestet. I doubt a 100k strings would be a memory problem.

QStringList tlist;
for(int i=0;i<100000;i++) 
    tlist.append("1234567890");

runs totally fine within my environment

Upvotes: 1

Related Questions