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.
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 addconst-qualification tocount, C4018 is not emitted even for your second comparison.