tags:

views:

278

answers:

3

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

m_vData.max_size() always returns -1. why would that happen?

+18  A: 

Probably because you're assigning it to a signed type before viewing. The return value of max_size is typically size_t which is an unsigned type. A straight conversion to say int on many platforms would return -1.

Try the following instead

std::vector<unsigned char>::size_type v1 = myVector.max_size();
JaredPar
Thanks man!!!!!
cchampion
+7  A: 

Note that max_size() returns a vector::size_type which is unsigned, so you're seeing a negative number due to converting it somewhere (you're really getting a very large unsigned number back).

The implementation is saying that it could handle vectors with that many elements (though I doubt that you'd actually get one allocated).

It's not not the number of elements in the vector (or currently reserved for the vector). You can get those numbers with vector::size() or vector::capacity().

Michael Burr
+4  A: 

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

ceo