tags:

views:

82

answers:

1

Consider this bit of code:

LARGE_INTEGER l;
size_t s;
if (s < l.QuadPart) return 1;
return 0;

When this is compiled under x64 it generates the C4018 signed/unsigned mismatch compiler warning (Ignore the uninitialized local variable warning).

The warning is fine, since QuadPart is LONGLONG which is signed and size_t is unsigned.

But when I compile this under 32-bit there is no warning? How come? Under 32-bit LONGLONG is still signed and size_t is unsigned.

+10  A: 

On 32-bit LONGLONG is equivalent to signed __int64 and size_t is equivalent to unsigned int. unsigned int has range that completely fits into signed __int64 range, so the compiler widens (does integer promotion) size_t to signed __int64 before the comparison and there's no warning.

On 64-bit LONGLONG is again equivalent to signed __int64 but size_t is equivalent to unsigned __int64, so now size_t no longer fits into LONGLONG and the compiler can't perform any kind of promotion automatically, hence the warning.

sharptooth