sizeof(长)在64位c++

时间:2023-01-03 12:03:15

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:

我已经下载了MinGW-64,所以现在我可以使用g++ 4.7.0(实验)来编译64位的Windows 7程序。但以下行:

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

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

打印4 8,不是8 8。g+ 4.6.0的文档说:

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

64位环境将int设置为32位,长,指针设置为64位

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

有人知道为什么sizeof(long)不是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:

被编辑为add:我的困惑之处在于,64位Windows的g+ 4.7.0还不是GNU编译器集合的正式组成部分。这是第一个32位长的64位版本,因此文档并不适用于它。实际上,如果您访问相关的web页面,IA-32/x86-64的完整条目包括以下内容:

...

6 个解决方案

#1


15  

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.

因为这不是必须的。c++标准只要求它(如果内存可用)至少32位宽,并且至少和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

MSVC(以及Windows使用的ABI)将long定义为32位宽,MingW也是如此,因为当编译器与主机操作系统一致时,编译器会更有用

#2


11  

On the microsoft windows OS you have LLP64 so the size of long is 32 bit. (see the table below)

在微软的windows操作系统上,你有LLP64,所以它的长度是32位。(见下表)

Quote from wikipedia:

引自*:

In 32-bit programs, pointers and data types such as integers generally have the same length; this is not necessarily true on 64-bit machines. Mixing data types in programming languages such as C and its descendants such as C++ and Objective-C may thus function on 32-bit implementations but not on 64-bit implementations. In many programming environments for C and C-derived languages on 64-bit machines, "int" variables are still 32 bits wide, but long integers and pointers are 64 bits wide. These are described as having an LP64 data model. Another alternative is the ILP64 data model in which all three data types are 64 bits wide, and even SILP64 where "short" integers are also 64 bits wide. However, in most cases the modifications required are relatively minor and straightforward, and many well-written programs can simply be recompiled for the new environment without changes. Another alternative is the LLP64 model, which maintains compatibility with 32-bit code by leaving both int and long as 32-bit. "LL" refers to the "long long integer" type, which is at least 64 bits on all platforms, including 32-bit environments.

在32位程序中,指针和数据类型(如整数)通常具有相同的长度;这在64位机器上不一定是正确的。将数据类型混合到编程语言(如C)及其后代语言(如c++和Objective-C)中,可能因此在32位实现上发挥作用,但在64位实现上却不起作用。在64位机器上的C和C派生语言的许多编程环境中,“int”变量仍然是32位宽,但是长整数和指针是64位宽。它们被描述为具有LP64数据模型。另一种选择是ILP64数据模型,其中所有三种数据类型都是64位宽,甚至是SILP64,其中“短”整数也是64位宽。然而,在大多数情况下,所需要的修改都是相对较小和简单的,许多编写良好的程序可以简单地为新环境重新编译,而不需要修改。另一种替代方法是LLP64模型,它通过保留int和long的32位代码来保持与32位代码的兼容性。“LL”是指“long long long long integer”类型,它在所有平台上至少是64位,包括32位环境。

Type           ILP64   LP64   LLP64
char              8      8       8
short            16     16      16
int              64     32      32
long             64     64      32
long long        64     64      64
pointer          64     64      64

#3


2  

MinGW is designed to build a WIN32 application and WIN32 headers/libraries assumes the long(or LONG) type to be 32 bits wide even on a 64bit Windows. Microsoft decided that otherwise so much of the existing Windows source codes should be changed. For example, the following structure uses LONG types.

MinGW设计用于构建WIN32应用程序,WIN32头/库假设长(或长)类型为32位宽,即使是在64位窗口上也是如此。微软决定,如果不这样做,那么现有的Windows源代码中有很多应该被修改。例如,下面的结构使用长类型。

typedef struct tagBITMAPINFOHEADER { 
...
  LONG biWidth; 
  LONG biHeight; 
...
} BITMAPINFOHEADER

;

;

#4


0  

It's OS specific. Windows still has size of long equal 32 bits

这是操作系统特定的。Windows的大小仍然是32位。

#5


0  

Most of Windows applications are written with the expectation that for all intents and purposes int=long=32 bits. I'm guessing MinGW is just making sure it's still the case and there're no surprises.

大多数Windows应用程序的编写都期望int=long=32位。我猜MinGW只是想确定情况还是这样,没有什么惊喜。

#6


0  

MinGW is designed to build Windows applications, and the Microsoft platform ABI specifies that int and long have the same size of 32 bits. If MinGW defined long differently from MSVC, most existing Windows apps that use long would break when compiled using MinGW.

MinGW设计用于构建Windows应用程序,而Microsoft platform ABI指定int和long的大小相同,为32位。如果MinGW定义的long与MSVC不同,那么大多数现有的Windows应用在使用MinGW编译时都会崩溃。

Having said that, Cygwin x86_64 does follow the LP64 convention on Windows, just like on Linux (source).

说到这一点,Cygwin x86_64确实遵循了Windows上的LP64约定,就像在Linux上一样(源代码)。

So you can use that to build a Windows app where the size of long is 8 bytes :)

所以你可以用它来创建一个Windows应用程序长8字节

Test case:

测试用例:

#include <stdio.h>
#include <windows.h>

int CALLBACK WinMain(HINSTANCE a, HINSTANCE b, LPSTR c, int d)
{
  char buf[100];
  snprintf(buf, sizeof(buf),
    "sizeof(int)=%d, sizeof(long)=%d, sizeof(long long)=%d\n",
     sizeof(int), sizeof(long), sizeof(long long));
  MessageBox(NULL, buf, "Cygwin Test", MB_OK);
  return 0;
}

Compile with: C:\cygwin64\bin\gcc.exe -mwindows -m64 cygwin-test.c -o cygwin-test

编译:C:\ cygwin64 \ bin \ gcc。exe -mwindows - m64 cygwin-test。c - o cygwin-test

Output:

输出:

sizeof(长)在64位c++

#1


15  

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.

因为这不是必须的。c++标准只要求它(如果内存可用)至少32位宽,并且至少和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

MSVC(以及Windows使用的ABI)将long定义为32位宽,MingW也是如此,因为当编译器与主机操作系统一致时,编译器会更有用

#2


11  

On the microsoft windows OS you have LLP64 so the size of long is 32 bit. (see the table below)

在微软的windows操作系统上,你有LLP64,所以它的长度是32位。(见下表)

Quote from wikipedia:

引自*:

In 32-bit programs, pointers and data types such as integers generally have the same length; this is not necessarily true on 64-bit machines. Mixing data types in programming languages such as C and its descendants such as C++ and Objective-C may thus function on 32-bit implementations but not on 64-bit implementations. In many programming environments for C and C-derived languages on 64-bit machines, "int" variables are still 32 bits wide, but long integers and pointers are 64 bits wide. These are described as having an LP64 data model. Another alternative is the ILP64 data model in which all three data types are 64 bits wide, and even SILP64 where "short" integers are also 64 bits wide. However, in most cases the modifications required are relatively minor and straightforward, and many well-written programs can simply be recompiled for the new environment without changes. Another alternative is the LLP64 model, which maintains compatibility with 32-bit code by leaving both int and long as 32-bit. "LL" refers to the "long long integer" type, which is at least 64 bits on all platforms, including 32-bit environments.

在32位程序中,指针和数据类型(如整数)通常具有相同的长度;这在64位机器上不一定是正确的。将数据类型混合到编程语言(如C)及其后代语言(如c++和Objective-C)中,可能因此在32位实现上发挥作用,但在64位实现上却不起作用。在64位机器上的C和C派生语言的许多编程环境中,“int”变量仍然是32位宽,但是长整数和指针是64位宽。它们被描述为具有LP64数据模型。另一种选择是ILP64数据模型,其中所有三种数据类型都是64位宽,甚至是SILP64,其中“短”整数也是64位宽。然而,在大多数情况下,所需要的修改都是相对较小和简单的,许多编写良好的程序可以简单地为新环境重新编译,而不需要修改。另一种替代方法是LLP64模型,它通过保留int和long的32位代码来保持与32位代码的兼容性。“LL”是指“long long long long integer”类型,它在所有平台上至少是64位,包括32位环境。

Type           ILP64   LP64   LLP64
char              8      8       8
short            16     16      16
int              64     32      32
long             64     64      32
long long        64     64      64
pointer          64     64      64

#3


2  

MinGW is designed to build a WIN32 application and WIN32 headers/libraries assumes the long(or LONG) type to be 32 bits wide even on a 64bit Windows. Microsoft decided that otherwise so much of the existing Windows source codes should be changed. For example, the following structure uses LONG types.

MinGW设计用于构建WIN32应用程序,WIN32头/库假设长(或长)类型为32位宽,即使是在64位窗口上也是如此。微软决定,如果不这样做,那么现有的Windows源代码中有很多应该被修改。例如,下面的结构使用长类型。

typedef struct tagBITMAPINFOHEADER { 
...
  LONG biWidth; 
  LONG biHeight; 
...
} BITMAPINFOHEADER

;

;

#4


0  

It's OS specific. Windows still has size of long equal 32 bits

这是操作系统特定的。Windows的大小仍然是32位。

#5


0  

Most of Windows applications are written with the expectation that for all intents and purposes int=long=32 bits. I'm guessing MinGW is just making sure it's still the case and there're no surprises.

大多数Windows应用程序的编写都期望int=long=32位。我猜MinGW只是想确定情况还是这样,没有什么惊喜。

#6


0  

MinGW is designed to build Windows applications, and the Microsoft platform ABI specifies that int and long have the same size of 32 bits. If MinGW defined long differently from MSVC, most existing Windows apps that use long would break when compiled using MinGW.

MinGW设计用于构建Windows应用程序,而Microsoft platform ABI指定int和long的大小相同,为32位。如果MinGW定义的long与MSVC不同,那么大多数现有的Windows应用在使用MinGW编译时都会崩溃。

Having said that, Cygwin x86_64 does follow the LP64 convention on Windows, just like on Linux (source).

说到这一点,Cygwin x86_64确实遵循了Windows上的LP64约定,就像在Linux上一样(源代码)。

So you can use that to build a Windows app where the size of long is 8 bytes :)

所以你可以用它来创建一个Windows应用程序长8字节

Test case:

测试用例:

#include <stdio.h>
#include <windows.h>

int CALLBACK WinMain(HINSTANCE a, HINSTANCE b, LPSTR c, int d)
{
  char buf[100];
  snprintf(buf, sizeof(buf),
    "sizeof(int)=%d, sizeof(long)=%d, sizeof(long long)=%d\n",
     sizeof(int), sizeof(long), sizeof(long long));
  MessageBox(NULL, buf, "Cygwin Test", MB_OK);
  return 0;
}

Compile with: C:\cygwin64\bin\gcc.exe -mwindows -m64 cygwin-test.c -o cygwin-test

编译:C:\ cygwin64 \ bin \ gcc。exe -mwindows - m64 cygwin-test。c - o cygwin-test

Output:

输出:

sizeof(长)在64位c++