This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

std::vector : integer overflow in size()


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?

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]