Max_size() for std::vector<char>
by Francois Fayard
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. Let me explain why I am puzzled.
Libc++ implementation of std::vector<T> 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. Therefore, the size of the vector is given by end_ - begin_. But the result of this difference is of type std::ptrdiff_t which is a signed integer of n bits on every implementation that I know of.
Therefore, this type can not store 2^n - 1, but only up to 2^(n-1) - 1. So, how come they can max_size() pretend to store more than 2^(n - 1) elements without breaking .size() ?
Thanks for your help.
9 years, 2 months