TonyK TonyK - 3 months ago 22
C++ Question

sizeof(long) in 64-bit C++

I have downloaded MinGW-64, so I can now compile 64-bit programs for Windows 7, using g++ 4.7.0 (experimental). But the following line:

cout << sizeof(long) << " " << sizeof(void*) << endl ;


prints
4 8
, not
8 8
. The documentation for g++ 4.6.0 says:


The 64-bit environment sets int to 32 bits and long and pointer to 64 bits


Does anybody know why
sizeof(long)
is not 8?

Edited to add: The source of my confusion was that g++ 4.7.0 for 64-bit Windows is not (yet) an official part of the GNU Compiler Collection. And it's the first 64-bit version with a 32-bit
long
, so the documentation simply doesn't apply to it. Indeed, if you go to the relevant web page, the full entry for IA-32/x86-64 consists of this:


...

Answer

Because it doesn't have to be. The C++ standard only requires that it is (if memory serves) at least 32 bits wide, and at least as big as int.

MSVC (and the ABI used by Windows) defines long to be 32 bits wide, and MingW follows suit because well, the compiler is a lot more useful when it agrees with the host OS

Comments