Qt World Summit: Register Today!

QByteArray maximum size

  • The size() function of QByteArray returns an int. The maximum value of an int is 2147483647 (which is 2GB) or unsigned 4294967295 (4GB). What happens if the size of the array is larger than that size, shouldn't it return a qint64? Or is it even possible to create QByteArrays larger than 2GB?

  • QByteArray supports up to 2GB of memory usage. Bigger blocks are not supported.

    Note that it will be hard to even acquire such a huge continious memory block in one go on most systems today. If you need bigger structures, you probably will be better off using your own custom memory structure.

  • Hm, interesting:

    QByteArray* ba = new QByteArray ( 3000000000, 'a' );

    if (ba)
        cout << "OK" ;
        cout << "NOT OK";
    cout << "\nsize of ba: " << ba->size();


    OK (pointer is created...)
    size of ba: 0 (???)

  • You are simply hit by a signed integer overflow and your number wraps around, actually passing <code>-1294967296</code> to the QByteArray constructor (the two's complement of <code>3000000000</code>), which leads to a zero-sized byte array (as a negative size is passed).

  • QBuffer works with qint64, and you can setBuffer(QByteArray). Does anyone know the behaviour if you now try to add more than 2GB to the QBuffer? Will it create a second QByteArray, or will it bomb out?

  • Why don't you just try and see? I mean: it should be easy enough to test this I think.

  • I currently only have 3GB memory on my workstation so I can't really test it. But thanks anyway, I don't think my program users will ever use more than 1GB or so.

Log in to reply