Vector Invalid Distribution Size

when I try to create and resize a vector to store the maximum number of elements (vector :: max_size ()), I get a debugging error at runtime:

"Invalid allocation size: 429467292"

I am wondering why u cannot resize if max_size () should return the maximum number of elements in a vector ...

vector<int> vc; vc.resize(vc.max_size()); 

I would also try to enable LARGADRESSAWARE: On in VS2010, but this does not help. Wonder if that was even the right thoguth ...

Anyone got it?

+4
source share
1 answer

max_size() - the absolute maximum number of elements that the vector can store. Using the default dispenser, it is usually std::numeric_limits<std::size_t>::max() / sizeof(T) . That is, this is the largest array of this type that you could create.

However, you could never actually allocate this array. The modules loaded by your program use some of the address spaces of your program, as well as the stacks of each thread. You will likely have other dynamically allocated objects in your program (allocated by you or the runtime). All this contributes to fragmentation of the address space, which means that the largest contiguous block of available address space is much smaller than the total amount of available address space.

In short, in practice it is impossible to select a vector with max_size() elements.

+5
source

All Articles