Why does the std :: vector max_size () function return -1?

I have std::vector<unsigned char> m_vData;

m_vData.max_size()always returns -1. why does this happen?

+3
source share
3 answers

Perhaps because before you browse, you assign it to a signed type. The return value of max_size is usually size_tone that is an unsigned type. Direct conversion to say int on many platforms will return -1.

Try instead

std::vector<unsigned char>::size_type v1 = myVector.max_size();
+18
source

Note that it max_size()returns vector::size_typeunsigned, so you see a negative number due to converting it somewhere (you really get a very large unsigned number back).

, ( , ).

( ). vector::size() vector::capacity().

+7

Note that on most platforms std::vector<unsigned char>::max_size, it will most likely be the same as std::numeric_limits<unsigned int>::max(), which of course is -1 when converting to a signed int.

+4
source

Source: https://habr.com/ru/post/1720917/


All Articles