Does the size of an int depend on the compiler and/or processor?(int 的大小是否取决于编译器和/或处理器?)
问题描述
整数的大小是否取决于编译器、操作系统和处理器?
Would the size of an integer depend upon the compiler, OS and processor?
推荐答案
这个问题的答案取决于我们愿意得到的实际考虑有多远.
The answer to this question depends on how far from practical considerations we are willing to get.
最终,理论上,C 和 C++ 中的一切都依赖于编译器,并且只依赖于编译器.硬件/操作系统根本不重要.编译器可以自由地实现任何厚度的硬件抽象层,并且可以完全模拟任何东西.没有什么可以阻止 C 或 C++ 实现实现任何大小和任何表示的 int
类型,只要它足够大以满足语言标准中指定的最低要求.这种抽象级别的实际示例很容易获得,例如基于虚拟机"平台的编程语言,如 Java.
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 any thickness 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++ 实现必须考虑源自底层硬件的某些考虑因素.出于这个原因,确保每个基本类型都基于硬件直接(或几乎直接)支持的某种表示很有意义.从这个意义上说,基本类型的大小确实取决于硬件.
However, C and C++ are intended to be highly efficient languages. 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.
换句话说,64 位硬件/OS 平台的特定 C 或 C++ 实现完全可以自由地将 int
实现为占用 128 位的 71 位 1 的补码整数类型内存,使用其他 57 位作为填充位,这些位总是需要存储编译器作者的女朋友的生日.这种实现甚至会具有一定的实用价值:它可以用于执行 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.
这篇关于int 的大小是否取决于编译器和/或处理器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:int 的大小是否取决于编译器和/或处理器?
基础教程推荐
- C++ 标准:取消引用 NULL 指针以获取引用? 2021-01-01
- 如何定义双括号/双迭代器运算符,类似于向量的向量? 2022-01-01
- 您如何将 CreateThread 用于属于类成员的函数? 2021-01-01
- 运算符重载的基本规则和习语是什么? 2022-10-31
- 设计字符串本地化的最佳方法 2022-01-01
- 调用std::Package_TASK::Get_Future()时可能出现争用情况 2022-12-17
- 如何在 C++ 中处理或避免堆栈溢出 2022-01-01
- C++ 程序在执行 std::string 分配时总是崩溃 2022-01-01
- C++,'if' 表达式中的变量声明 2021-01-01
- 什么是T&&(双与号)在 C++11 中是什么意思? 2022-11-04