<html><head><meta http-equiv="Content-Type" content="text/html charset=windows-1252"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Hi Csaba,<div class=""><br class=""></div><div class="">I know that the type of the objet returned by the .size() method is std::size_t which is an unsigned integer of n bits (where n = 32 on x86 and n = 64 on x86-64). My claim is that libstdc++ (and every standard library that I know of) runs into undefined behaviour for vectors of size >= 2^(n-1) when the size method is called.</div><div class=""><br class=""></div><div class="">To make things concrete, let’s suppose that:</div><div class="">- You are on a 32 bits system (n = 32)</div><div class="">- You construct a std::vector<char> of size 2^31, which represents 2GB of memory. We assume that this memory is available so the constructor does not throw any exception</div><div class="">- You call the .size() method on that object</div><div class=""><br class=""></div><div class="">Here is the code available in the libstdc++ that comes with gcc 4.9.2</div><div class=""><br class=""></div><div class="">      size_type<br class="">      size() const _GLIBCXX_NOEXCEPT<br class="">      { return size_type(this->_M_impl._M_finish - this->_M_impl._M_start); }<br class=""></div><div class=""><br class=""></div><div class="">As you can see, it takes the difference of the pointers this->_M_impl._M_finish and this->_M_impl._M_start. The second one points to the first element of the array and the first one points to the one after the last element of the array. Now, if you look at the C++11 standard, 5.7 (Additive operators), point 6, about the difference of two pointers:</div><div class=""><br class=""></div><div class=""><div class="">"When two pointers to elements of the same array object are subtracted, the result is the difference of the subscripts of the two array elements. The type of the result is an implementation-defined signed integral type; this type shall be the same type that is defined as std::ptrdiff_t in the <cstddef> header (18.2). <i class="">As with any other arithmetic overflow, if the result does not fit in the space provided, the behavior is undefined….</i>”</div></div><div class=""><br class=""></div><div class="">As you can see, the difference of 2 pointers is of type std::ptrdiff_t which is a signed integer of n bits. Therefore, the maximum value it can store is 2^(n-1) - 1. In our example, 2^31 is too big for std::ptrdiff_t, and the standard says that this is undefined behaviour. The fact that this number is casted to std::size_t after that operation is irrelevant.</div><div class=""><br class=""></div><div class="">François</div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On 15 Feb 2015, at 21:52, Csaba Raduly <<a href="mailto:rcsaba@gmail.com" class="">rcsaba@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class="">Hi Francois,<br class="">The return type of size() is size_t, which is unsigned, so it can<br class="">represent values up to 2^n-1.<br class=""><br class=""><a href="http://en.cppreference.com/w/cpp/container/vector/max_size" class="">http://en.cppreference.com/w/cpp/container/vector/max_size</a> says:<br class=""><br class="">"This value is typically equal to<br class="">std::numeric_limits<size_type>::max(), and reflects the theoretical<br class="">limit on the size of the container. "<br class=""><br class="">Csaba<br class=""></div></blockquote></div><br class=""></div></body></html>