Take the 2-minute tour ×

Suppose I have this (C++ or maybe C) code:

vector<int> my_vector;
    my_vector[i] = 0;

I don't care if it's done right. The important part is in the for-loop declaration. The compiler gives a signed/unsigned mismatch for this, since size() returns an unsigned int, not a signed one. How important is it to change i to unsigned? I declare loop counters as ints out of habit, but if this is a potential error I'll force myself to get out of the habit.

share|improve this question

6 Answers 6

up vote 10 down vote accepted

I would say it's very important - you should be compiling with warnings as errors, and strive to fix all warnings. If you leave problems like this in your code, it is easy to get into a habit of ignoring warnings, or letting false positives like this drown out warnings that indicate real problems.

In this case, for this specific error, it's probably not a big deal - on a 32-bit platform you'd have to have more than 2 billion entries in the vector before the unsigned would wrap into a negative signed value. To get a vector like this would exhaust all of your memory, so it's probably not possible to get into a state where signed/unsigned mismatch would matter.

share|improve this answer
One danger lies in real warnings drowing in warnings that arn't "dangerous". Always get rid of warnings. –  nos Jun 29 '09 at 19:47
On Linux you can ask for a data of 2GB with no problem, only if you'll actually use it to it's size, the O.S will exhaust the memory, but the 'size' can be declared and used as 2GB before that. so it may be a problem should you ask such a vector size. –  Liran Orevi Jun 29 '09 at 19:50
@liran - It's an array of int's. 2 billion entries at 4 bytes each is 8GB. –  Michael Jun 29 '09 at 19:51
@Michael, good point :). On char my example stands. –  Liran Orevi Jun 29 '09 at 19:55

Technically, i should be a vector<int>::size_type. You should get in the habit of using typedefs in your code:

typedef vector<int> VectorType;
VectorType my_vector;
for (VectorType::size_type i = 0; i < my_vector.size(); i++) {
    my_vector[i] = 0;

Now, if we change it to a deque, we only change one line. Even if it's some custom container that has a wacky size_type, you get the warm, fuzzy feeling that everything will be ok. And that's worth a lot. Even with just unsigned/signed, there are some tricky promotion issues with using signed/unsigned conversion that will inevitably come back to bite you.

share|improve this answer

This may be important in the unlikely event that the size of the vector exceeds INT_MAX. If the size of the vector is greater than the maximum value that can be represented in a signed int, then your loop will never terminate.

share|improve this answer

Well, its important because signed integers have sign, so i could go all the way up becoming a negative value, then no matter how big it is, it would still be less than size(), which doesnt have any sign.

11111111 < 10000000

share|improve this answer

On most cases of your example, it won't matter. but when your program doesn't work, the first thing you do (or should) is to make sure there are no warnings, so it's a chance not worth taking.

make sure there are as few warnings as possible.

share|improve this answer

As said above, use vector::size_type; or use an iterator to loop through your vector. Make sure to handle all of your own warnings as errors.

share|improve this answer

Your Answer


