2

Some weird result encountered in VC++2010:

enum dow :unsigned long long {mon=0x800022223333ULL,tue};
int _tmain(int argc, _TCHAR* argv[])
{
    dow a=mon;
    unsigned long long b=0x800022223333ULL;
    printf("%d\n",sizeof(a));
    printf("%llx\n",a); // a is 32bit or 64 bit???
    printf("%llx\n",(unsigned long long)a);
    printf("%llx\n",b);
    return 0;
}

I got some unexpected result:

8
1ff1d3f622223333
800022223333
800022223333

The 1ff1d3f622223333 is incorrect. After inspecting the generated assembly code, I found that the compiler was passing a 32bit 0x22223333 in the printf for a which was then incorrectly interpreted as 64bit by the printf format specification. Hence the garbage 1ff1d3f6 was inserted. Why is it so?

EDIT forgot to say that it was compiled as a 32bit exe with both Release and Debug Configuration.

JavaMan
  • 4,954
  • 4
  • 41
  • 69

1 Answers1

1

This appears to be a bug in that version of Visual Studio. The following code:

#include <cstdio>

enum dow :unsigned long long {mon=0x800022223333ULL,tue};
int main()
{
    dow a=mon;
    unsigned long long b=0x800022223333ULL;
    printf("%d\n",sizeof(a));
    printf("%llx\n",a); // a is 32bit or 64 bit???
    printf("%llx\n",(unsigned long long)a);
    printf("%llx\n",b);
    return 0;
}

produces a similar result in VS2010:

8
22223333
800022223333
800022223333

However, it looks like it's been fixed in later releases, the same code run in VS2015 Express gives:

8
800022223333
800022223333
800022223333

Since our version of VS2010 has all patches installed, it looks like this was never fixed in that version. My suggestion therefore (if you really need those large enumerations) is to upgrade.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953