Make Visual Studio warn for unsigned comparisons

2019-09-14 09:01发布

问题:

This code, when compiled with Visual Studio 2010

#include <cstdlib>
#include <cstdio>

int numbers[] = { 23, 24, 25, 25, 28, 20, 20 };

int main(void) {
    int d = -1, x=0;
    size_t count = sizeof(numbers) / sizeof(numbers[0]);

    if (d <= (sizeof(numbers) / sizeof(numbers[0]))-2)
        x = numbers[d+1];

    if (d <= count-2)
        x = numbers[d+1];
}

gives me a signed/unsigned mismatch warning on if (d <= count-2) but not on if (d <= (sizeof(numbers) / sizeof(numbers[0]))-2). Why is this? I have enabled all warnings.

回答1:

It is a bug that the Visual C++ compiler does not emit warning C4018 in this case. See the Microsoft Connect bug report, "warning C4018 (signed/unsigned mismatch) for sizeof operator."

The issue appears to be only indirectly related to usage of sizeof. The compiler also fails to emit the warning in other cases where a constant value of unsigned type is involved in the comparison. For example, if you add const-qualification to count, C4018 is not emitted even for your second comparison.



回答2:

My guess is that it treats 2 as an int so promotes (sizeof(numbers) / sizeof(numbers[0]))-2) to an int because of the brackets and so the comaprison is int < int



回答3:

size_t is defined as unsigned so you get a signed unsigned conflict.

Please note, as sizeof is evaluated during compile time, and is being replaced by a constant value, it does not complain about sign mismatch, but in the other expression, you explicitly convert the value to a size_t variable which can possibly overflow when you try to compare with a signed number.