Alex Rivera | Logout

size_t vs int warning

Asked 2011-07-05T04:47:39.103
51

I am getting following warning always for following type of code.

std::vector v;
for ( int i = 0; i < v.size(); i++) {
}

warning C4267: 'initializing' : conversion from 'size_t' to 'int', possible loss of data

I understand that size() returns size_t, just wanted to know is this safe to ignore this warning or should I make all my loop variable of type size_t

Edit
Report

1 Answer

6

The problem is that you're mixing two different data types. On some architectures, size_t is a 32-bit integer, on others it's 64-bit. Your code should properly handle both.

since size() returns a size_t (not int), then that should be the datatype you compare it against.

std::vector v;
for ( size_t i = 0; i < v.size(); i++) {
}
answered 2011-07-05T07:30:21.053

Your Answer