问题描述
为什么 int
在64位编译器上通常为32位?当我开始编程时,我被教过int通常与底层架构的宽度相同。我同意这也是有道理的,我发现未指定的宽度整数与底层平台一样宽是合乎逻辑的(除非我们讨论的是8或16位机器,其中的范围很小
几乎不适用。)
Why is int
typically 32 bit on 64 bit compilers? When I was starting programming, I've been taught int is typically the same width as the underlying architecture. And I agree that this also makes sense, I find it logical for a unspecified width integer to be as wide as the underlying platform (unless we are talking 8 or 16 bit machines, where such a small range for int
will be barely applicable).
后来我学会了 int
通常是32位在大多数64位平台上。所以我想知道这是什么原因。为了存储数据,我更喜欢显式指定数据类型的宽度,因此这留下了 int
的通用用法,这不会提供任何性能优势,至少在我的系统上我对32位和64位整数具有相同的性能。这就留下了二进制内存占用空间,虽然不是很多,但会略有减少......
Later on I learned int
is typically 32 bit on most 64 bit platforms. So I wonder what is the reason for this. For storing data I would prefer an explicitly specified width of the data type, so this leaves generic usage for int
, which doesn't offer any performance advantages, at least on my system I have the same performance for 32 and 64 bit integers. So that leaves the binary memory footprint, which would be slightly reduced, although not by a lot...
推荐答案
错误的选择实现者的一部分?
Bad choices on the part of the implementors?
严重的是,根据标准,普通投资具有执行架构建议的
自然尺寸
环境,这意味着64位
机器上的64位 int
。人们可以轻易地争辩说其他任何东西都是
不符合要求。但实际上,问题更复杂:
从32位 int
切换到64位 int
将不允许
大多数程序处理大型数据集或其他任何东西(不像
从16位切换到32位);大多数程序可能受到其他考虑因素限制的
。它会增加数据集的
大小,从而减少局部性并减慢
程序的速度。
Seriously, according to the standard, "Plain ints have thenatural size suggested by the architecture of the executionenvironment", which does mean a 64 bit int
on a 64 bitmachine. One could easily argue that anything else isnon-conformant. But in practice, the issues are more complex:switching from 32 bit int
to 64 bit int
would not allowmost programs to handle large data sets or whatever (unlike theswitch from 16 bits to 32); most programs are probablyconstrained by other considerations. And it would increase thesize of the data sets, and thus reduce locality and slow theprogram down.
最后(也可能是最重要的),如果 int
是64位,
short
必须是16位或
32位,并且你无法指定另一个(除了
,其中typedef在<$ c $中) c>< stdint.h> ,意图是这些
只应在非常特殊的情况下使用)。
我怀疑这是主要动机。
Finally (and probably most importantly), if int
were 64 bits,short
would have to be either 16 bits or32 bits, and you'ld have no way of specifying the other (exceptwith the typedefs in <stdint.h>
, and the intent is that theseshould only be used in very exceptional circumstances).I suspect that this was the major motivation.
这篇关于为什么int在64位编译器上通常是32位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!