Is there a way to do this in C++ without having things crash on runtime?
Right now I am declaring
vector<vector<int> > myvec(veclength);
How can I crank up veclength as high as it will go (properly)? Even at 10^7 it crashes when I should have more than enough computer memory.
This should take take approximately 250 MiB of space1 (or less, depending on architecture) so memory definitely isn’t the problem here, and neither should max_size
, which would be in the order of 1017 (≈ 264∕8+8+8).
I should mention that I corroborated these calculations by looking at the implementations of std::vector
in GCC' libstdc++ and LLVM's libc++, and by testing on a live system. The calculated values correspond 1:1 to the real implementations, and the OP’s code works flawlessly with veclength = 10e7
.
I therefore conclude that the real cause is elsewhere.
1) Calculated by approximating the size of each individual vector by three 64 bit integers to denote begin pointer, size and capacity respectively, and assuming that an empty vector has a default capacity of 0. Actual implementations may differ but probably not by much.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With