4

when I try to create and resize a vector to hold the maximum number of items (vector::max_size()) i get a debug error during runtime :

"Invalid allocation size: 429467292"

Im wondering why u cant resize this, if max_size() should return the maximum number of item in the vector...

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

I'd also try to enable LARGADRESSAWARE:On in VS2010, but that does not help. Wondering if this was even a right thoguth...

Anyone got a clue?

1
  • 1
    Other issues aside, you probably compiling for 32-bit. 429467292 is probably the maximum index that can be handled, but in reality you won't be able to get anywhere need that due to 32-bit memory limitations. Commented Aug 27, 2012 at 20:25

1 Answer 1

5

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

However, you could never actually allocate that large an array. The modules loaded by your program use up some of your program's address space, as do the stacks of each thread. You'll probably have other dynamically allocated objects in your program (either allocated by you or the runtime). These all contribute to address space fragmentation, which means that the largest contiguous block of available address space is much smaller than the total amount of available address space.

In short, it is not possible in practice to allocate a vector with max_size() elements.

Sign up to request clarification or add additional context in comments.

2 Comments

isn't it somehow possible to achieve this by using file mapping or something like this in windows?... Could one redirect the memory used by a std::vector to this file-mapping machanism?
Memory mapped I/O doesn't suddenly give your program more address space. In a 32-bit process, you have a 4GB address space. std::vector requires one big contiguous allocation, so address space fragmentation is the limiting factor. In a 64-bit process you have the same problem in theory, but in practice because the address space is so large, it's not as much of an issue.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.