C++ int 的大小是否取决于编译器和/或处理器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2331751/
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
Does the size of an int depend on the compiler and/or processor?
提问by Vijay
Would the size of an integer depend upon the compiler, OS and processor?
整数的大小是否取决于编译器、操作系统和处理器?
回答by AnT
The answer to this question depends on how far from practical considerations we are willing to get.
这个问题的答案取决于我们愿意得到的实际考虑有多远。
Ultimately, in theory, everything in C and C++ depends on the compiler and only on the compiler. Hardware/OS is of no importance at all. The compiler is free to implement a hardware abstraction layer of anythickness and emulate absolutely anything. There's nothing to prevent a C or C++ implementation from implementing the int
type of any size and with any representation, as long as it is large enough to meet the minimum requirements specified in the language standard. Practical examples of such level of abstraction are readily available, e.g. programming languages based on "virtual machine" platform, like Java.
最终,从理论上讲,C 和 C++ 中的一切都取决于编译器,而且只取决于编译器。硬件/操作系统根本不重要。编译器可以自由地实现任何厚度的硬件抽象层并模拟任何东西。没有什么可以阻止 C 或 C++ 实现实现int
任何大小和任何表示的类型,只要它足够大以满足语言标准中指定的最低要求。这种抽象级别的实际示例很容易获得,例如基于“虚拟机”平台的编程语言,如 Java。
However, C and C++ are intended to be highly efficientlanguages. In order to achieve maximum efficiency a C or C++ implementation has to take into account certain considerations derived from the underlying hardware. For that reason it makes a lot of sense to make sure that each basic type is based on some representation directly (or almost directly) supported by the hardware. In that sense, the size of basic types do depend on the hardware.
但是,C 和 C++ 旨在成为高效的语言。为了实现最高效率,C 或 C++ 实现必须考虑源自底层硬件的某些考虑因素。出于这个原因,确保每个基本类型都基于硬件直接(或几乎直接)支持的某种表示很有意义。从这个意义上说,基本类型的大小确实取决于硬件。
In other words, a specific C or C++ implementation for a 64-bit hardware/OS platform is absolutely free to implement int
as a 71-bit 1's-complement signed integral type that occupies 128 bits of memory, using the other 57 bits as padding bits that are always required to store the birthdate of the compiler author's girlfriend. This implementation will even have certain practical value: it can be used to perform run-time tests of the portability of C/C++ programs. But that's where the practical usefulness of that implementation would end. Don't expect to see something like that in a "normal" C/C++ compiler.
换句话说,64 位硬件/OS 平台的特定 C 或 C++ 实现完全可以自由地实现int
为 71 位 1 的补码有符号整数类型,占用 128 位内存,使用其他 57 位作为填充位总是需要存储编译器作者女朋友的生日。这种实现甚至会具有一定的实用价值:它可以用于执行 C/C++ 程序可移植性的运行时测试。但这就是该实现的实际用途将结束的地方。不要期望在“普通”C/C++ 编译器中看到类似的东西。
回答by minjang
Yes, it depends on both processors (more specifically, ISA, instruction set architecture, e.g., x86 and x86-64) and compilers including programming model. For example, in 16-bit machines, sizeof (int) was 2 bytes. 32-bit machines have 4 bytes for int
. It has been considered int
was the nativesize of a processor, i.e., the size of register. However, 32-bit computers were so popular, and huge number of software has been written for 32-bit programming model. So, it would be very confusing if 64-bit computer would have 8 bytes for int
. Both Linux and Windows remain 4 bytes for int
. But, they differ in the size of long
.
是的,它取决于处理器(更具体地说,ISA、指令集架构,例如 x86 和 x86-64)和编译器,包括编程模型。例如,在 16 位机器中,sizeof (int) 是 2 个字节。32 位机器有 4 个字节用于int
. 它被认为int
是处理器的本机大小,即寄存器的大小。但是,32 位计算机如此流行,并且已经为 32 位编程模型编写了大量软件。因此,如果 64 位计算机有 8 个字节的int
. Linux 和 Windows 都为int
. 但是,它们的大小不同long
。
Please take a look at the 64-bit programming model like LP64for most *nix and LLP64for Windows:
请查看 64 位编程模型,例如大多数 *nix 的LP64和Windows 的LLP64:
- http://www.unix.org/version2/whatsnew/lp64_wp.html
- http://en.wikipedia.org/wiki/64-bit#64-bit_data_models
- http://www.unix.org/version2/whatsnew/lp64_wp.html
- http://en.wikipedia.org/wiki/64-bit#64-bit_data_models
Such differences are actually quite embarrassing when you write code that should work both on Window and Linux. So, I'm always using int32_t
or int64_t
, rather than long
, via stdint.h.
当您编写的代码应该同时在 Window 和 Linux 上运行时,这种差异实际上非常令人尴尬。所以,我总是通过stdint.h使用int32_t
or int64_t
,而不是。long
回答by Jerry Coffin
Yes, it would. Did they mean "which would it depend on: the compiler or the processor"? In that case the answer is basically "both." Normally, int
won't be bigger than a processor register (unless that's smaller than 16 bits), but it could be smaller (e.g. a 32-bit compiler running on a 64-bit processor). Generally, however, you'll need a 64-bit processor to run code with a 64-bit int.
是的,会的。他们的意思是“它取决于哪个:编译器还是处理器”?在这种情况下,答案基本上是“两者兼而有之”。通常,int
不会比处理器寄存器大(除非它小于 16 位),但它可以更小(例如,在 64 位处理器上运行的 32 位编译器)。但是,通常您需要一个 64 位处理器来运行具有 64 位 int 的代码。
回答by SCW
Based on some recent research I have done studying up for firmware interviews:
根据最近的一些研究,我已经完成了固件面试的研究:
The most significant impact of the processors bit architecture ie, 8bit, 16bit, 32bit, 64bit is how you need to most efficiently store each byte of information in order to best compute variables in the minimum number of cycles.
处理器位架构(即 8 位、16 位、32 位、64 位)的最显着影响是您需要如何最有效地存储每个字节的信息,以便在最少的周期数内最好地计算变量。
The bit size of your processor tells you what the natural word length the CPU is capable of handling in one cycle. A 32bit machine needs 2 cycles to handle a 64bit double if it is aligned properly in memory. Most personal computers were and still are 32bit hence the most likely reason for the C compiler typical affinity for 32bit integers with options for larger floating point numbers and long long ints.
处理器的位大小告诉您 CPU 在一个周期内能够处理的自然字长是多少。如果 32 位机器在内存中正确对齐,则需要 2 个周期来处理 64 位双精度。大多数个人计算机过去和现在仍然是 32 位,因此最可能的原因是 C 编译器对 32 位整数具有典型的亲和力,并具有更大的浮点数和长整数的选项。
Clearly you can compute larger variable sizes so in that sense the CPU's bit architecture determines how it will have to store larger and smaller variables in order to achieve best possible efficiency of processing but it is in no way a limiting factor in the definitions of byte sizes for ints or chars, that is part of compilers and what is dictated by convention or standards.
显然,您可以计算更大的变量大小,因此从这个意义上说,CPU 的位架构决定了它必须如何存储越来越大的变量以实现最佳处理效率,但这绝不是字节大小定义的限制因素对于整数或字符,这是编译器的一部分,由约定或标准规定。
I found this site very helpful, http://www.geeksforgeeks.org/archives/9705, for explaining how the CPU's natural word length effects how it will chose to store and handle larger and smaller variable types, especially with regards to bit packing into structs. You have to be very cognizant of how you chose to assign variables because larger variables need to be aligned in memory so they take the fewest number of cycles when divided by the CPU's word length. This will add a lot of potentially unnecessary buffer/empty space to things like structs if you poorly order the assignment of your variables.
我发现这个网站非常有帮助,http://www.geeksforgeeks.org/archives/9705,用于解释 CPU 的自然字长如何影响它如何选择存储和处理更大和更小的变量类型,特别是关于位打包进入结构。您必须非常清楚如何选择分配变量,因为较大的变量需要在内存中对齐,因此当除以 CPU 的字长时,它们占用的周期数最少。如果您对变量的分配排序不当,这将为结构等内容添加许多潜在的不必要的缓冲区/空白空间。
回答by zar
The simple and correct answer is that it depends on the compiler. It doesn't mean architecture is irrelevant but the compiler deals with that, not your application. You could say more accurately it depends on the (target) architecture of the compiler for example if its 32 bits or 64 bits.
简单而正确的答案是它取决于编译器。这并不意味着架构无关紧要,而是编译器处理它,而不是您的应用程序。您可以更准确地说它取决于编译器的(目标)架构,例如它是 32 位还是 64 位。
Consider you have windows application that creates a file where it writes an int
plus other things and reads it back. What happens if you run this on both 32 bits and 64 bits windows? What happens if you copy the file created on 32 bits system and open it in 64 bits system?
考虑您有 Windows 应用程序,它创建一个文件,在其中写入int
其他内容并将其读回。如果在 32 位和 64 位窗口上运行它会发生什么?如果您复制在 32 位系统上创建的文件并在 64 位系统中打开它会发生什么?
You might think the size of int will be different in each file but no they will be the same and this is the crux of the question. You pick the settings in compiler to target for 32 bits or 64 bits architecture and that dictates everything.
您可能认为每个文件中 int 的大小会有所不同,但它们不会相同,这是问题的关键。您在编译器中选择设置以针对 32 位或 64 位架构,这决定了一切。
回答by mukesh
size of data type basically depends upon the type of compiler and compilers are designed on the basis of architecture of processors so externally data type can be considered to be compiler dependent.for ex size of integer is 2 byte in 16 bit tc compiler but 4 byte in gcc compiler although they are executed in same processor
数据类型的大小基本上取决于编译器的类型,并且编译器是根据处理器架构设计的,因此外部数据类型可以被认为是编译器相关的。例如,整数的大小在 16 位 tc 编译器中为 2 字节,但为 4 字节在 gcc 编译器中,尽管它们在同一个处理器中执行
回答by bruziuz
http://www.agner.org/optimize/calling_conventions.pdf
http://www.agner.org/optimize/calling_conventions.pdf
"3 Data representation" contains good overview of what compilers do with integral types.
“3 数据表示”很好地概述了编译器如何处理整型。
回答by mahesh
Data Types Size depends on Processor, because compiler wants to make CPU easier accessible the next byte. for eg: if processor is 32bit, compiler may not choose int size as 2 bytes[which it supposed to choose 4 bytes] because accessing another 2 bytes of that int(4bytes) will take additional CPU cycle which is waste. If compiler chooses int as 4 bytes CPU can access full 4 bytes in one shot which speeds your application.
数据类型大小取决于处理器,因为编译器想让 CPU 更容易访问下一个字节。例如:如果处理器是 32 位,编译器可能不会选择 int 大小为 2 个字节[它应该选择 4 个字节],因为访问该 int(4bytes) 的另外 2 个字节将花费额外的 CPU 周期,这是浪费。如果编译器选择 int 作为 4 字节,CPU 可以一次性访问完整的 4 字节,从而加速您的应用程序。
Thanks
谢谢
回答by KawaiKx
Size of the int is equal to the word-length that depends upon the underlying ISA. Processor is just the hardware implementation of the ISA and the compiler is just the software-side implementation of the ISA. Everything revolves around the underlying ISA. Most popular ISA is Intel's IA-32 these days. it has a word length of 32bits or 4bytes. 4 bytes could be the max size of 'int' (just plain int, not short or long) compilers. based on IA-32, could use.
int 的大小等于取决于底层 ISA 的字长。处理器只是 ISA 的硬件实现,编译器只是 ISA 的软件端实现。一切都围绕着底层的 ISA。目前最流行的 ISA 是 Intel 的 IA-32。它的字长为 32 位或 4 字节。4 个字节可能是“int”(只是普通的 int,而不是短或长)编译器的最大大小。基于 IA-32,可以使用。
回答by Ashish
Yes , I found that size of int in turbo C was 2 bytes where as in MSVC compiler it was 4 bytes.
是的,我发现 turbo C 中 int 的大小是 2 个字节,而在 MSVC 编译器中它是 4 个字节。
Basically the size of int is the size of the processor registers.
基本上 int 的大小是处理器寄存器的大小。