Why is the capacity of qvector smaller than that of std:: vector?
-
@Christian-Ehrlicher said in Why is the capacity of qvector smaller than that of std:: vector?:
because in Qt5 'int' is used as index
220.011.222 - fits perfectly into an int.
@jsulm oh, correct 🙂
-
@John-Van said in Why is the capacity of qvector smaller than that of std:: vector?:
QVector<char>ware0 (220011222);
Works just fine for me. Where exactly do you see this error? I guess in QtCreator editor? Or is it compiler error?
Also, what is your platform, OS and Qt version?@jsulm
Also both lines fine for me under Linux, 64-bit, gcc 11.4, Qt 5.15, Creator 6.0.2.
I can multiply both sizes by x10 and also fine.
If I multiply both by x100 I get "overflow conversion from long int to int" error from compiler (not Creator), which is what I would expect. -
QVector<double>ware1 (275111222)
actually crashes during runtime MSVC 2019 Qt 5.12.9
with 5.15.8 I get
which is even more unusual.
-
QVector<double>ware1 (275111222)
actually crashes during runtime MSVC 2019 Qt 5.12.9
with 5.15.8 I get
which is even more unusual.
@J-Hilk std will crash also as you don't have 800MB of contiguous memory.
-
QVector<double>ware1 (275111222)
actually crashes during runtime MSVC 2019 Qt 5.12.9
with 5.15.8 I get
which is even more unusual.
@J-Hilk
OOI, if you click Abbrechen to debug do you get to see what lineabort()
is being called from?@Christian-Ehrlicher
Since it's 200 million *double
which is 8 bytes, I think it's actually 1.6GB contiguous which is required?
Still an awful lot, as you said one wonders why someone would want so much (and contiguous).... -
@J-Hilk std will crash also as you don't have 800MB of contiguous memory.
@Christian-Ehrlicher said in Why is the capacity of qvector smaller than that of std:: vector?:
@J-Hilk std will crash also as you don't have 800MB of contiguous memory.
I tested that, that actually works with std::vector!
But you're right, thats 800 mb!!! I did not realise !!
-
@Christian-Ehrlicher said in Why is the capacity of qvector smaller than that of std:: vector?:
@J-Hilk std will crash also as you don't have 800MB of contiguous memory.
I tested that, that actually works with std::vector!
But you're right, thats 800 mb!!! I did not realise !!
@J-Hilk said in Why is the capacity of qvector smaller than that of std:: vector?:
thats 800 mb!!! I did not realise !!
Then it will crash on first access when the memory needs to be allocated.
-
@J-Hilk said in Why is the capacity of qvector smaller than that of std:: vector?:
thats 800 mb!!! I did not realise !!
Then it will crash on first access when the memory needs to be allocated.
@Christian-Ehrlicher probably
-
@Christian-Ehrlicher probably
@J-Hilk no crash on Ubuntu 22.04.
#include <vector> #include <iostream> int main() { std::vector< double > ware1( 275111222 ); for ( auto & value : ware1 ) { value = 1.0; } std::cout << ware1[ 0 ] << " " << ware1.size() << std::endl; return 1; }
output:
1 275111222 -
@J-Hilk no crash on Ubuntu 22.04.
#include <vector> #include <iostream> int main() { std::vector< double > ware1( 275111222 ); for ( auto & value : ware1 ) { value = 1.0; } std::cout << ware1[ 0 ] << " " << ware1.size() << std::endl; return 1; }
output:
1 275111222@JoeCFD said in Why is the capacity of qvector smaller than that of std:: vector?:
o crash on Ubuntu 22.04
We already discussed the reason... You have enough contiguous memory for this test.
-
@JoeCFD said in Why is the capacity of qvector smaller than that of std:: vector?:
o crash on Ubuntu 22.04
We already discussed the reason... You have enough contiguous memory for this test.
@Christian-Ehrlicher
Ok. In that case, maybe it is better to add exception handlingint main() { try { std::vector< double > ware1( 275111222 ); for ( auto & value : ware1 ) { value = 1.0; } std::cout << ware1[ 0 ] << " " << ware1.size() << std::endl; } catch( std::bad_alloc & ex ) { std::cerr << "bad allocation caught " << ex.what(); } return 1; }
-
@Christian-Ehrlicher
Ok. In that case, maybe it is better to add exception handlingint main() { try { std::vector< double > ware1( 275111222 ); for ( auto & value : ware1 ) { value = 1.0; } std::cout << ware1[ 0 ] << " " << ware1.size() << std::endl; } catch( std::bad_alloc & ex ) { std::cerr << "bad allocation caught " << ex.what(); } return 1; }
-
-
@J-Hilk std will crash also as you don't have 800MB of contiguous memory.
@Christian-Ehrlicher said in Why is the capacity of qvector smaller than that of std:: vector?:
std will crash also as you don't have 800MB of contiguous memory.
"Contiguous" is usually not the problem. With a 64bit OS and paging you have a huge address space which can be used to create contiguous addresses. PC processors don't use 64bit pointers in hardware and IIRC it was only 52bit pointers initially. This still would give you 4 petabytes of address space per process. You only need enough free memory which can then be put anywhere inside that huge address space. You would have to produce some serious fragmentation (or other really huge data) to fill up that address space.
However, if you only have 8 GB of RAM you'll quickly might run of space if you request 800MB and a lot of other things are also running.