std::vector : integer overflow in size()

Bo Persson bop@gmb.dk
Sun Feb 15 11:12:00 GMT 2015


On 2015-02-15 10:07, Francois Fayard wrote:
> Hi,
>
> I am puzzled by the result of std::vector<char>::max_size() on the n = 32 and n = 64 bits system I have tested. The result is 2^n − 1. To me, the libstdc++ can't handle vectors whose size are bigger than 2^(n-1) - 1. Let me explain why.
>
> Every implementation of std::vector<T> that I know of, libstdc++ included, has three members of type T*: begin_, end_, capacity_. begin_ points to the first value of the vector and end_ points to the one after the last. The size() method takes the difference of those pointers which is of type std::ptrdiff_t (which is then casted to a std::size_t). This type is a signed integer of n bits. Therefore, it can not store the integer 2^n − 1, but only up to 2^(n − 1) − 1. That's why I would expect this last number for max_size(). Is it a bug or something that I overlooked?

The max_size() function doesn't tell you the largest size you can use, 
it just tells you that going beyond max_size() definitely doesn't work. 
In practice you will not even get close - memory fragmentation, the size 
of program code and current x64 chips not implementing all 64 address 
bits are just some of the limitations.

The standards committee is aware of this, but didn't find it meaningful 
to try to improve the function.

http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-closed.html#197


  Bo Persson



More information about the Libstdc++ mailing list