2

Possible Duplicate:
What is the bit size of long on 64-bit Windows?

Why is it that when I compile as x64 in Microsoft Visual Studio 2010, sizeof(unsigned int) == 4? Doesn't it must Shouldn't it be 8 == sizeof(int)? I change platform in configuration manager and in command_line_compiler all settings are x64, but size of unsigned int = 4.

What did I do wrong?

Community
  • 1
  • 1
Kirill Golikov
  • 1,354
  • 1
  • 13
  • 27
  • 1
    Note that even on Unix 64-bit systems, `sizeof(int) == sizeof(unsigned int)` and both are 4 (not 8). On Unix, `long` (and `unsigned long`) are 64-bit quantities; so too are `long long` and `unsigned long long`. This is the LP64 (`long` and pointers are 64-bit) model. Windows 64 leaves both `int` and `long` at 32 bits, and only `long long` and pointers are 64-bit quantities (LLP64 — `long long` and pointers are 64-bit). – Jonathan Leffler Nov 11 '12 at 21:32

3 Answers3

1

Doesn't it must = 8 = sizeof(int)

No, that need not be. The size of the various integer types is up to the implementation, the standard only prescribes (in section 5.2.4.2) some minimal ranges.

For example, a char must have at least 8 bits, a short int and an int (also the unsigned versions thereof, which must have the same size and alignment requirements as the corresponding signed type) must have at least 16 bits, long must have at least 32 bits, and long long at least 64.

Even on 64-bit systems, it is common that int (and unsigned int) are 32-bit types.

Whether a long is 32 bits or 64 depends, it's 64 bits on most (if not all) 64-bit linux systems, and 32 bits on Windows 64 bit.

Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
0

Language standard doesn't tell about the sizeof(int) or any data types. Its implementation defined.

But it should follow this rule:

1<=sizeof(char) < sizeof(short) <= sizeof(int) <= sizeof(long) < sizeof(long long)
Omkant
  • 9,018
  • 8
  • 39
  • 59
  • 1
    “1<=sizeof(char)” -> The standard tells us a little bit more than that (C99 6.5.3.4:3). “sizeof(char) < sizeof(short)” -> The standard does not say that. “sizeof(long) < sizeof(long long)” -> The standard does not say that. – Pascal Cuoq Nov 11 '12 at 22:34
-1

Doesn't it must = 8 = sizeof(int)?

If this is, in correct English, supposed to mean "should it not have a size equal to that of int"? then the answer is: yes, the standard requires that an unsigned integral type and its corresponding signed type be of the same size. If that's not the case, then Visual Studio's compiler is not conformant (surprise, surprise...).

However, it's possible that both int and unsigned int are 4 byte long - there's nothing particularly strange in that, even on a 64-bit implementation. Probably long or long long (and their unsigned counterparts) are 8 byte long.