int 的大小是否取决于编译器和/或处理器?

Does the size of an int depend on the compiler and/or processor?(int 的大小是否取决于编译器和/或处理器?)
本文介绍了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 的大小是否取决于编译器和/或处理器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Algorithm to convert RGB to HSV and HSV to RGB in range 0-255 for both(将 RGB 转换为 HSV 并将 HSV 转换为 RGB 的算法,范围为 0-255)
How to convert an enum type variable to a string?(如何将枚举类型变量转换为字符串?)
When to use inline function and when not to use it?(什么时候使用内联函数,什么时候不使用?)
Examples of good gotos in C or C++(C 或 C++ 中好的 goto 示例)
Significance of ios_base::sync_with_stdio(false); cin.tie(NULL);(ios_base::sync_with_stdio(false) 的意义;cin.tie(NULL);)
Is TCHAR still relevant?(TCHAR 仍然相关吗?)