(ALL Containers Use INT.) What can save live data in memory if the data length is 'qint64' ?
-
OLD TITLE: How can QIODevice read 'qint64 maxSize' and return a QByteArray when the interface of QByteArray is 'int' based NOT 'qint64' based?
I am very confused. If I pass a 'qint64' to a function that takes an 'int', then doesn't the qint64 become an int???
The entire interface to QByteArray has all 'int's as parameters/return values like int size(), resize(int), etc.. So therefore how can a QByteArray be longer than the maximum length of an int?
Furthermore, the interface to QBuffer (in addition to QIODevice) is all 'qint64' based yet QBuffer is built on an internal QByteArray.
So Is it possible to set the size of QByteArray to be larger than an int when the API is built on ints?
-
- Yes, a QByteArray can't hold data bigger than MAX_INT bytes
- QBuffer uses qint64 because it derives from QIODevice and has to provide the same interface as a QIODevice. Despite using qint64 in the interface, a QBuffer too can't hold data bigger than MAX_INT bytes.
As to why QIODevice uses qint64, but QByteArray uses int, I'm afraid I have to guess: QByteArray is only something like a better alternative for the simple char * - so it's interface is in terms of the more straightforward int, the type you would normally use to index char *s. QIODevice is a generic read-write interface that's used as a base for file and network access, so it's probably designed to be able to handle potentially huge chunks of data.
-
roopeshchand.. Ok, if like I think - a QByteArray can't hold data bigger than MAX_INT (maximum size of a regular 'int' integer), then what is the meaning of this interface in QIODevice:
@
QByteArray QIODevice::read ( qint64 maxSize )Reads at most maxSize bytes from the device, and returns the data read as a QByteArray.
@If it reads a maxSize amount of data of qint64 then how will that fit inside a QByteArray?
If the other classes are using the QByteArray that has an 'int' interface, then why do the other classes have 'qint64' interfaces? Anyway, what is one to use to store something in memory that contains data of length longer than int?
-
peppe: I changed the title of the post. I am under the impression that QByteArray cannot save all data if the data size is larger than the maximum size of an integer. All of the containers seem to return sizes or lengths of 'int'.
OLD TITLE: How can QIODevice read ‘qint64 maxSize’ and return a QByteArray when the interface of QByteArray is ‘int’ based NOT ‘qint64’ based?
NEW TITLE: (ALL Containers Use INT.) What can save live data in memory if the data length is ‘qint64’ ?
-
Don't use that read(qint64) overload, but create a suitable buffer and use the read(char*, qint64) one. The limitation of QByteArray is not a problem, since noone pretends to use it to manage a contiuous memory chunk longer than 2GB.
In other news, what's the use case of reading more than 2^31-1 bytes at once from a device?
-
OK pepe. But does your comment mean that a QByteArray can hold data up to 2GB? I thought QByteArray can only hold up to 32K bytes since the size is based on integer.
Some of the standard examples are having webpages 'readAll()' into a QByteArray through QNetworkReply. But many web pages have more data than 32K or more characters than 32K so if put into a QString, the webpage would be amputated.
-
[quote author="Iama Hummingbird" date="1315081309"]OK pepe. But does your comment mean that a QByteArray can hold data up to 2GB? I thought QByteArray can only hold up to 32K bytes since the size is based on integer.[/quote]
"int" is a 32 bit signed integer on all platforms supported by Qt. Therefore, you can index (and store) up to 2^32-1 bytes inside a QByteArray. Which means up to 2GB - 1B. That's why I was asking for an use case -- allocating a single, huge chunk of 2GB is not that common.
[quote]
Some of the standard examples are having webpages 'readAll()' into a QByteArray through QNetworkReply. But many web pages have more data than 32K or more characters than 32K so if put into a QString, the webpage would be amputated.
[/quote]See above for the 32K part. But I think that this is quite a corner case, and you won't wait to buffer 2GB in memory before attempting to read it all at once -- you will read it as soon as possible (i.e. just after readyRead is emitted). This way you can safely download files even bigger than 2GB.