What is the maximum size of a QList?

Has anyone encountered the maximum size for a QList?

I have a QList of pointers to my objects and found that it silently causes an error when it reaches 268,435,455 points, which is exactly 28 bits. I expected it to have at least a 31-bit maximum size (minus one bit, since size () returns a signed integer) or a maximum size of 63 bits on my 64-bit computer, but that doesn't seem to be the case. I confirmed this in a minimal example by doing QList<void*> mylist; mylist.append(0);in a counting loop.

To repeat the question, what is the actual maximum QList size? If this is not really 2 ^ 32-1, then why? Is there a workaround?

I am running the 64-bit version of Windows Qt 4.8.5 for MSVC2010.

+4
source share
4 answers

While the other answers provide a useful attempt to explain the problem, none of them answer the question or miss a point. Thanks to everyone for helping track the problem.

As Ali Mofrad noted, the error caused by the error std::bad_allocwhen the QList cannot allocate extra space in my call QList::append(MyObject*), Here, where this happens in the Qt source code:

qlist.cpp: line 62:
static int grow(int size)         //size = 268435456
{
    //this is the problem line
    volatile int x = qAllocMore(size * sizeof(void *), QListData::DataHeaderSize) / sizeof(void *);
    return x;                     //x = -2147483648
}

qlist.cpp: line 231:
void **QListData::append(int n)   //n = 1
{
    Q_ASSERT(d->ref == 1);
    int e = d->end;
    if (e + n > d->alloc) {
        int b = d->begin;
        if (b - n >= 2 * d->alloc / 3) {
            //...
       } else {
            realloc(grow(d->alloc + n));    //<-- grow() is called here
        }
    }
    d->end = e + n;
    return d->array + e;
}

grow() (268,435,456) sizeof(void*) (8), QList. , 268435456 * 8 +2,147,483,648, int32 -2,147,483,648 int32, grow() . , std:: realloc() QListData::realloc(int), .

, ddriver, QList::reserve(), , QList - .

, QList 2 ^ 28-1 , , 2 ^ 31-1, .

+2

- QList? QList , , 268 435 455 , 28 . , 31- ( , size() ) 63 64- , , , .

, int, 2 ^ 31 - 1. 4 ( 32- ), 2 ^ 29 - 1. , , . reserve() ().

, Win32 . , , (1G 2G).

? ?

+2

QList void *.

, 2 28 , void *, 2 30 32- 2 31 64- . , .

? , ?


void * , - , .

QList void *, (.. sizeof(T) <= sizeof(void*)), memmove. new, . , , . Q_DECLARE_TYPEINFO.

:

  • , void * (char; int float 64 ..), 50 75%
  • , void * (double on 32bit, QVariant,...), ( )
  • QList , QVector one
  • , .

QVector. , Qt API QList ( ++ 11, QList QVector...)

+1

32- Ubuntu 4 , qt4.8.6. - 268 435 450

Windows7 32bit 4 , qt4.8.4. - 134 217 722

: 'std:: bad_alloc'

#include <QCoreApplication>
#include <QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QList<bool> li;
    for(int i=0; ;i++)
    {
        li.append(true);
        if(i>268435449)
            qDebug()<<i;
    }
    return a.exec();
}

:

268435450

ending a call after calling an instance of 'std :: bad_alloc'
what (): std :: bad_alloc

0
source

All Articles