What should I do about the "signed/unsigned mismatch" warning in C++ code like this:
for (int i = vector.size() - 1; i >= 0; --i) // OK
{
if (i < vector.size() / 2) // warning C4018: '<': signed/unsigned mismatch
// ...
}
(A contrived example, but it demonstrates the problem.)
How do I deal with "signed/unsigned mismatch" warnings (C4018)? says to use size_t
for the loop variable, but this doesn't work for a decrementing loop terminating at 0. It compiles without warning, but the integer overflows (or is it underflows?) at runtime and becomes 4294967295.