[cfe-dev] libc++: max_size() of a std::vector

Csaba Raduly rcsaba at gmail.com
Sun Feb 15 12:52:32 PST 2015


Hi Francois,
The return type of size() is size_t, which is unsigned, so it can
represent values up to 2^n-1.

http://en.cppreference.com/w/cpp/container/vector/max_size says:

"This value is typically equal to
std::numeric_limits<size_type>::max(), and reflects the theoretical
limit on the size of the container. "

Csaba


On Sat, Feb 14, 2015 at 6:21 PM, Francois Fayard
<fayard.francois at icloud.com> 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. Let me explain why I am puzzled.
>
>
>
> Every implementation of std::vector<T> that I know of, libc++ 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 is computes 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 the integer 2^n - 1, but only up to 2^(n - 1) - 1.
>
>
>
> I would expect this last number for max_size(). Is it a bug or something that I overlooked?
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev



-- 
GCS a+ e++ d- C++ ULS$ L+$ !E- W++ P+++$ w++$ tv+ b++ DI D++ 5++
The Tao of math: The numbers you can count are not the real numbers.
Life is complex, with real and imaginary parts.
"Ok, it boots. Which means it must be bug-free and perfect. " -- Linus Torvalds
"People disagree with me. I just ignore them." -- Linus Torvalds




More information about the cfe-dev mailing list