C++ 64 位机器上 int 和 sizeof int 指针的大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20721294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Size of int and sizeof int pointer on a 64 bit machine
提问by Itzik984
I was just wondering how can I know if my laptop is 64 or 32 bit machine. (it is a 64).
我只是想知道我怎么知道我的笔记本电脑是 64 位还是 32 位机器。(这是一个 64)。
So, I thought about printing the following:
所以,我想打印以下内容:
int main()
{
printf("%d",sizeof(int));
}
and the result was 4, which seemed weird (since it is a 64 bit machine)
结果是 4,这看起来很奇怪(因为它是 64 位机器)
But, when I printed this:
但是,当我打印此内容时:
int main()
{
printf("%d",sizeof(int*));
}
the result was 8, which made more sense.
结果是 8,这更有意义。
The question is:
问题是:
Since I'm using a 64 bit machine, shouldn't a primitive type such as int should use 8 bytes
由于我使用的是 64 位机器,因此 int 等原始类型不应该使用 8 个字节
(64 bit) and by that sizeof int should be 8? Why isn't it so?
(64 位),那么 sizeof int 应该是 8?为什么不是这样?
And why is the int* size is 8?
为什么 int* 大小是 8?
A bit confused here,
这里有点糊涂
so thanks in advance.
所以提前致谢。
回答by ScarletAmaranth
No, the sizeof(int)
is implementation defined, and is usually 4 bytes.
不,sizeof(int)
是实现定义的,通常是 4 个字节。
On the other hand, in order to address more than 4GB of memory (that 32bit systems can do), you need your pointers to be 8 bytes wide. int*
just holds the address to "somewhere in memory", and you can't address more than 4GB of memory with just 32 bits.
另一方面,为了寻址超过 4GB 的内存(32 位系统可以做到),您的指针需要为 8 字节宽。int*
只是将地址保存到“内存中的某个地方”,并且仅使用 32 位就无法寻址超过 4GB 的内存。
回答by Rahul Tripathi
Size of a pointer should be 8 byte on any 64-bit C/C++ compiler, but the same is not true for the size of int.
在任何 64 位 C/C++ 编译器上,指针的大小都应该是 8 字节,但对于 int 的大小,情况并非如此。
The wikihas a good explanation on that:
在维基有一个很好的解释:
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.[citation needed] 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.
在 64 位机器上的 C 和 C 派生语言的许多编程环境中,“int”变量仍然是 32 位宽,但长整数和指针是 64 位宽。这些被描述为具有 LP64 数据模型。另一种选择是 ILP64 数据模型,其中所有三种数据类型都是 64 位宽,甚至 SILP64,其中“短”整数也是 64 位宽。 [需要引用] 然而,在大多数情况下,所需的修改相对较小且直接,许多编写良好的程序可以简单地为新环境重新编译而无需更改。另一种选择是 LLP64 模型,它通过将 int 和 long 保留为 32 位来保持与 32 位代码的兼容性。“LL”指的是“long long integer”类型,在所有平台上至少为64位,
回答by chux - Reinstate Monica
The sizeof(int)
, sizeof(int*)
, and "machine size", though often correlated to each other, can each be independently smaller, the same or larger than the others. About the only C requirement is that they be at least 16 bits (or so) - other than that, it compiler dependent for the sizeof(int)
, sizeof(int*)
.
的sizeof(int)
,sizeof(int*)
和“机器大小”,虽然经常彼此关联,可以各自独立地小于其他中,相同的或更大。关于唯一的 C 要求是它们至少为 16 位(左右)-除此之外,它依赖于sizeof(int)
, 的编译器sizeof(int*)
。
(Although maybe a pointer must be at least an int size. Hmmm)
(虽然也许一个指针必须至少是一个 int 大小。嗯)
回答by gnasher729
Programmers like to have integer types of 1, 2, 4 and 8 bytes or 8, 16, 32 and 64 bits. There are only two integer types that could be smaller than int: char and short. If int was 64 bits, then you couldn't have all three of the sizes 8, 16 and 32 bits. That's why compilers tend to make int = 32 bits, so you can have char = 8 bit, short = 16 bit, int = 32 bit, long long = 64 bit and long = 32 bit or 64 bit.
程序员喜欢拥有 1、2、4 和 8 字节或 8、16、32 和 64 位的整数类型。只有两种整数类型可以小于 int:char 和 short。如果 int 是 64 位,那么您不可能同时拥有 8、16 和 32 位这三种尺寸。这就是为什么编译器倾向于使 int = 32 位,所以你可以有 char = 8 位、short = 16 位、int = 32 位、long long = 64 位和 long = 32 位或 64 位。
回答by SuperJOE
Because of size_t was define as
因为 size_t 被定义为
typedef unsigned int size_t;
You should display it with %zu, %u or %lu instead of %d.
您应该使用 %zu、%u 或 %lu 而不是 %d 来显示它。
printf("%zu\n", sizet);
printf("%u\n", sizet);
printf("%lu\n", sizet);