When using vector, “Out of Memory” show.
To fix it, I use max_size() to check, then reserve or push_back.
If the max_size() is bigger the reserved value, it should be ok, but it is no! Then what’s the meaning of max_size()?
I compile below demo in windows 7 and Visual studio 2010. My PC has 4GB RAM. When the reseverd is 1/2 of max_size(), it fails.
max_size()=2^32/sizeof(CPoint3D)-1=268435455
It’s ok when 1/4 of max_size() in the demo. In my real project, It’s ok until 1/10.
What’s the right of max size of vector, how to enlarge it?
I got the “out of memory” error when I push a lot of elements into std::vector. To avoid the error, I checked with vector::max_size() first, and use vector::reserve() to pre-allocate the memory. However, it doesn’t work. In a demo project, the error occurs when I reserve 1/4 of the max_size. In the real project, the error occurs when I reserve 1/10 of it. I’m running Windows 7 and compiling with Visual Studio 2010. My computer has 4GB RAM.
If max_size doesn’t work, how do I find out the maximum number of elements I can allocate for a vector?
The problem is that vector tries to allocate a contiguous block of memory, which might not be available at that time, even though the total available memory may be much larger.
I would suggest to use
std::dequeas it does not require to allocate a contiguous block of memory.