Should QVector be able to hold one billion items?
Paste and run the following code in a debug x64 build:
- for(int i = 0; i < 1000000000; ++i)
- {
- vec.push_back(i);
- }
Should this work ok?
It seems to throw a bad_alloc.
10 replies
Do you allocate the vector like you show in the example? Could you try this:
- for(int i = 0; i < 1000000000; ++i)
- {
- vec.push_back(i);
- }
Telling the vector up front how bit it must be, will save you an insane amount of shifting the data around…
However, I think I would not use the Qt container classes for such data blobs. This is the point where the copy-on-write of the Qt containers becomes a burden rather than a benefit.
Thanks for the suggestion however it still throws an exception.
It will even throw an exception just doing a reserve as follows:
- vec.reserve(1000000000);
I have investigated the problem and it seems to be as follows:
1. QVector<T>::malloc(int aalloc) is called by vec.reserve(), where aalloc is 1 billion.
2. In QVector<T>::malloc we call:
The first argument ‘size’ is:
4 + (1000000000 – 1) * 4 = 4000000000 which is greater than the positive range of a signed int.
Looking at the declaration of QVectorData::allocate we see that the first argument is an int:
So it looks like it has an overflow error.
Note: even 550 million messages throws the exception, but below this the allocate passes, presumably because the size is less than the positive range of an int.
this has nothing to do with int and long ?
can you try with:vec.reserve(1000000000L);
Actually, no. QVector (and all other container classes) only work with integers (int, not long or long long). So, I guess it is just outside of the Qt containers specs to try to strore that many items in one. I would recommend using a different container class for that, from outside of Qt.
[edit: answer put outside quote, Eddy]
You must log in to post a reply. Not a member yet? Register here!




