64 位 C++ 中的 sizeof(long) [英] sizeof(long) in 64-bit C++

查看:45
本文介绍了64 位 C++ 中的 sizeof(long)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经下载了 MinGW-64,所以我现在可以使用 g++ 4.7.0(实验性)为 Windows 7 编译 64 位程序.但是下面这行:

cout <<<大小(长)<<" " <<大小(空*)<<结束;

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

<块引用>

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

有谁知道为什么 sizeof(long) 不是 8?

编辑添加:我困惑的根源是用于 64 位 Windows 的 g++ 4.7.0(还)不是 GNU 编译器集合的官方部分.它是第一个具有 32 位 long 的 64 位版本,因此文档根本不适用于它.事实上,如果您访问相关网页IA-32/x86-64 包括:

<块引用>

...

解决方案

因为它不是必须的.C++ 标准只要求它(如果有内存的话)至少 32 位宽,并且至少与 int 一样大.

MSVC(以及 Windows 使用的 ABI)将 long 定义为 32 位宽,MingW 紧随其后,因为编译器在与主机操作系统一致时更有用

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:

...

解决方案

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

这篇关于64 位 C++ 中的 sizeof(long)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆