Max_size() for std::vector<char>

Sam Varshavchik mrsam at courier-mta.com
Sat Feb 14 19:27:56 UTC 2015


Francois Fayard writes:

> Hi Varshavchik,
>
> I am sure nobody will encounter the bug on x86-64 before a long time. But  
> the bug might show up on 32 bit platforms.
>
> Let's say that you instantiate a std::vector<char> of length 2^31 on such a  
> platform. It is far from common, but something that can happen as it  
> represents 2 GB of memory. All the pointers will be set correctly, and the  
> object can run smoothly with iterators.
>
> Now, if you call the size() method, you'll have to make a difference of 2  
> pointers: begin_ and end_ = begin_ + 2^31. This difference has a type of  
> std::ptrdiff_t, which is a 32 bit signed integer on most 32 bits platforms.  
> This type can't store 2^31. It is just too big. Therefore, we go in the  
> "undefined behavior" territory.

You may have a point.

The value of std::vector<char>::max_size() is, of course, defined by each  
C++ compiler that supplies its implementation of the C++ library. So, in  
this case, where I looked at what gcc does, this question should be deferred  
to gcc.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <https://lists.fedorahosted.org/pipermail/libcxx-users/attachments/20150214/53b52b2d/attachment.sig>


More information about the libcxx-users mailing list