Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sizeof(long) in 64-bit C++

Tags:

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:

...

like image 823
TonyK Avatar asked Sep 30 '11 07:09

TonyK


People also ask

Is long 64-bit C?

Windows: long and int remain 32-bit in length, and special new data types are defined for 64-bit integers.

Is long long a 64-bit?

int , long , ptr , and off_t are all 32 bits (4 bytes) in size. int is 32 bits in size. long , ptr , and off_t are all 64 bits (8 bytes) in size.

Is long 32 or 64-bit?

LP32 or 2/4/4 (int is 16-bit, long and pointer are 32-bit)

What is sizeof long in C?

unsigned long. 8 bytes. 0 to 18446744073709551615. To get the exact size of a type or a variable on a particular platform, you can use the sizeof operator. The expressions sizeof(type) yields the storage size of the object or type in bytes.


1 Answers

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

like image 168
jalf Avatar answered Sep 20 '22 19:09

jalf