为什么某些编译器中 int 的大小会有所不同? [英] Why does the size of an int vary in some compilers?

查看:45
本文介绍了为什么某些编译器中 int 的大小会有所不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读以下资源,它说 int/指针的大小可能因编译器而异:

Reading the following resource it says the size of an int/pointer can vary depending on the compiler:

http://www.c4learn.com/c-programming/c-size-of-pointer-variable/

这是为什么?

我知道 C 只定义了类型应该包含的最小和最大数量,但是为什么一个编译器会选择将例如 int 设置为 2 个字节而另一个设置为 4 个字节?一个比另一个有什么优势?

I understand C defines only the min and max number of what a type should hold, but why would one compiler choose to set for example int to 2 bytes and another at 4? What would be the advantage of one over another?

推荐答案

虽然为什么"可以用因为标准这么说"来回答,但可以提出标准可以写成不同的论点,以保证具体尺寸.

Whilst the "why" can be answered with "Because the standard says so", one could make the argument that the standard could be written differently, to guarantee a particular size.

然而,C 和 C++ 的目的是在所有机器上生成非常快的代码.如果编译器必须确保 int 是该机器的非自然大小",则需要额外的指令.在几乎所有情况下,这不是必需的,您所关心的只是它足以满足我想做的事情".因此,为了给编译器一个生成好代码"的好机会,标准只指定了最小大小,避免编译器必须生成额外代码"以使 int(和其他类型)在一种非常具体的方式.

However, the purpose of C and C++ is to produce very fast code on all machines. If the compiler had to make sure that an int is a "unnatural size" for that machine, it would require extra instructions. For nearly all circumstances, that is not required, all you'd care about is that it's "big enough for what I want to do". So, to give the compiler a good chance to generate "good code", the standard specifies only minimum sizes, avoiding the compiler having to generate "extra code" to make int (and other types) behave in a very specific way.

C 和 C++ 的众多优势之一是编译器可以针对各种机器,从小型 8 位和 16 位微控制器到大型 64 位多核处理器,如 PC 中的处理器.当然,还有一些 18、24 或 36 位机器.如果你的机器有一个 36 位的本机大小,你不会很高兴,因为一些标准是这样说的,由于额外的指令,你在整数数学中获得一半的性能,并且不能使用一个的前 4 位int...

One of the many benefits of C and C++ is that there are compilers to target a vast range of machines, from little 8- and 16-bit microcontrollers to large, 64-bit multi-core processors like the ones in a PC. And of course, some 18, 24 or 36-bit machines too. If your machine has a 36-bit native size, you wouldn't be very happy if, because some standard says so, you get half the performance in integer math due to extra instructions, and can't use the top 4 bits of an int...

具有 8 位寄存器的小型微处理器通常支持执行 16 位加法和减法(也许还有乘法和除法),但是 32 位数学将涉及将这些指令加倍 [以及更多的乘法和除法运算]划分].所以 16 位整数(2 字节)在如此小的处理器上会更有意义——特别是因为内存可能也不是很大,所以为每个整数存储 4 个字节有点浪费.在 32 位或 64 位机器中,内存范围很可能要大得多,因此具有较大的整数并不是什么太大的缺点,并且 32 位整数运算与较小的运算速度相同(并且在某些情况下更好" - 例如在 x86 中,16 位简单数学运算(例如加法或减法)需要额外的前缀字节来表示使这个 16 位",因此 16 位整数的数学运算将占用更多代码空间).

A small microprocessor with 8-bit registers often have support to do 16-bit additions and subtractions (and perhaps also multiplication and divide), but 32-bit math would involve doubling up on those instructions [and more work for multiplication and divide]. So 16-bit integers (2 byte) would make much more sense on such a small processor - particularly since memory is probably not very large either, so storing 4 bytes for every integer is a bit of a waste. In a 32- or 64-bit machine, memory range is most likely a lot larger, so having larger integers isn't that much of a drawback, and 32-bit integer operations are the same speed as smaller ones (and in some cases "better" - for example in x86, a 16-bit simple math operation such as addition or subtraction requires an extra prefix byte to say "make this 16-bit", so math on 16-bit integers would take up more code-space).

这篇关于为什么某些编译器中 int 的大小会有所不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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