Reputation: 17467
The std::vector::resize member function is like this:
void resize (size_type n);
Per my understanding, size_type
should be a 64-bit long
type on a 64-bit
platform. But compiling the following program:
#include <iostream>
#include <climits>
#include <vector>
using namespace std;
vector<char> v;
int main() {
// your code goes here
v.resize(INT_MAX +1);
for (auto i = 0; i < v.size(); i++ ) {
cout << i << endl;
}
return 0;
}
The following warning is generated:
g++ -std=c++11 hello.cpp
hello.cpp: In function ‘int main()’:
hello.cpp:11:19: warning: integer overflow in expression [-Woverflow]
v.resize(INT_MAX +1);
So the size_type
is still 32-bit int
even though we're working on a 64-bit
platform?
Upvotes: 0
Views: 777
Reputation: 15501
The vector's size_type
is probably a typedef for allocator::size_t
which is (probably) a typedef for std::size_t which is an unsigned type. The generated warning has nothing to do with the vector's resize()
signature. You are overflowing the max integer limit and your INT_MAX + 1
expression invokes undefined behaviour. Also the for
loop deduces the type of i
to be int
which will also issue a warning when comparing signed and unsigned values. If you really want to you can cast to size_type
and add 1
:
v.resize(static_cast<std::vector<char>::size_type>(INT_MAX) + 1);
and append the u
literal to initial value inside the for
loop:
for (auto i = 0u; i < v.size(); i++)
You can get the underlying type name with:
std::cout << typeid(std::vector<char>::size_type).name();
Upvotes: 3