c 中数据类型的大小
数据类型的大小取决于硬件架构还是编译器?
我想知道哪些因素真正影响确定数据类型的大小?
Is size of a datatype hardware architecture dependent or compiler dependent?
I want to know what factors really influence in determining the size of a datatype?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
编译器(更准确地说是“实现”)可以自由选择大小,但要遵守 C 标准的限制(例如 int 必须至少为 16 位)。编译器可以选择遵守其他标准,例如 POSIX,这可以添加更多约束。例如,我认为 POSIX 规定所有数据指针的大小相同,而 C 标准则非常满意
sizeof(int*) != sizeof(char*)
。在实践中,编译器编写者的决策很大程度上受到体系结构的影响,因为除非有充分的理由,否则他们希望实现高效且可互操作。处理器制造商或操作系统供应商经常发布一种称为“C ABI”的东西,它告诉您(除其他外)类型有多大以及它们如何存储在内存中。编译器从来没有义务遵循其架构的标准 ABI,并且 CPU 通常具有多个通用 ABI,但要直接从一个编译器的代码调用另一个编译器的代码,两个编译器都必须使用相同的 ABI。因此,如果您的 C 编译器不在 Windows 上使用 Windows ABI,那么您需要额外的包装器来调用 Windows dll。如果您的编译器支持多个平台,那么它很可能在不同平台上使用不同的 ABI。
您经常会看到缩写用于指示正在使用的 ABI 中的哪一个。例如,当 64 位平台上的编译器说它是 LP64 时,这意味着
long
和指针是 64 位,而忽略int
是 32 位。如果显示 ILP64,则意味着int
也是 64 位。最后,这更像是编译器编写者从明智的选项菜单中进行选择的情况,而不是随意挑选数字。但实现总是可以自由地做任何它喜欢做的事。如果您想为 x86 编写一个编译器来模拟具有 9 位字节和 3 字节字的机器,那么 C 标准允许它。但就操作系统而言,你只能靠自己了。
The compiler (more properly the "implementation") is free to choose the sizes, subject to the limits in the C standard (for instance int must be at least 16 bits). The compiler optionally can subject itself to other standards, like POSIX, which can add more constraints. For example I think POSIX says all data pointers are the same size, whereas the C standard is perfectly happy for
sizeof(int*) != sizeof(char*)
.In practice, the compiler-writer's decisions are strongly influenced by the architecture, because unless there's a strong reason otherwise they want the implementation to be efficient and interoperable. Processor manufacturers or OS vendors often publish a thing called a "C ABI", which tells you (among other things), how big the types are and how they're stored in memory. Compilers are never obliged to follow the standard ABI for their architecture, and CPUs often have more than one common ABI anyway, but to call directly from code out of one compiler to code out of another, both compilers have to be using the same ABI. So if your C compiler doesn't use the Windows ABI on Windows, then you'd need extra wrappers to call into Windows dlls. If your compiler supports multiple platforms, then it quite likely uses different ABIs on different platforms.
You often see abbreviations used to indicate which of several ABIs is in use. So for instance when a compiler on a 64 bit platform says it's LP64, that means
long
and pointers are 64bit, and by omissionint
is 32bit. If it says ILP64, that meansint
is 64bit too.In the end, it's more a case of the compiler-writer choosing from a menu of sensible options, than picking numbers out of the air arbitrarily. But the implementation is always free to do whatever it likes. If you want to write a compiler for x86 which emulates a machine with 9-bit bytes and 3-byte words, then the C standard allows it. But as far as the OS is concerned you're on your own.
大小最终由编译器决定。例如,Java 有一组固定的大小(8、16、32、64),而 C 为其各种类型提供的大小组部分取决于其运行的硬件;即编译器做出选择,但(除了像 Java 这样数据类型明确独立于底层硬件的情况)很大程度上受到硬件提供的影响。
The size is ultimately determined by the compiler. e.g. Java has a fixed set of sizes (8,16,32,64) while the set of sizes offered by C for its various types depends in part on the hardware it runs on; i.e. the compiler makes the choice but (except in cases like Java where datatypes are explicitly independent of underlying hardware) is strongly influenced by what the hardware offers.
不同数据类型的大小取决于编译器,以及其配置依赖(不同的编译器,或者同一编译器的不同切换,在某些机器上可能有不同的大小)。
通常编译器与其安装的硬件相匹配......所以你可以说类型的大小也依赖于硬件。制作一个在 48 位机器上发出 16 位指针的编译器会适得其反。
但是可以使用计算机上的编译器来创建旨在在不同大小的不同计算机上运行的程序。
The size of different data types is compiler, and its configuration, dependent (different compilers, or different switches to the same compiler, on some machine can have different sizes).
Usually the compiler is matched to the hardware it is installed on ... so you could say that the sizes of types are also hardware dependent. Making a compiler that emits 16-bit pointers on a machine where they are 48-bits is counter productive.
But it is possible to use a compiler on a computer to create a program meant to run on a different computer with different sizes.
它取决于目标硬件架构、操作系统以及可能的编译器。
英特尔编译器按如下方式调整长整数的大小:
此处作为链接显示 微软 Visual C++ 编译器。
It depends on the target hardware architecture, operation system and possibly the compiler.
The intel compiler sizes a long integer as follows:
Here as link to show the sizes on microsoft visual c++ compiler.
“本机”数据类型的大小取决于编译器。虽然这又受到硬件的影响,但我不会开始猜测。
看一下
- 该标头具有独立于平台的 typedef,可以满足您可能有的任何需求。The size of the "native" datatypes is up to the compiler. While this in turn is influenced by the hardware, I wouldn't start guessing.
Have a look at
<stdint.h>
- that header has platform-independent typedef's that should cater for whatever needs you might have.你的问题的答案是是,我会解释一下。
考虑常见的存储类型,即 size_t、int64_t 等。这些类型是在编译时根据您构建的体系结构决定的(在大多数情况下定义)。没有它们吗?不用担心,编译器已经弄清楚了 int 的基本含义以及 unsigned 如何影响它。
一旦标准 C 标头计算出所需的字长,一切都会根据您的系统进行调整。
当然,除非您碰巧进行交叉编译,否则它们由您指定的任何体系结构决定(或定义)。
简而言之,您的编译器(主要是预处理器)将调整类型以适应目标体系结构。无论是您正在使用的,还是您正在交叉编译的。
那是如果我正确理解你的问题。这是该语言提供的少数“神奇”抽象之一,也是它通常被称为“可移植汇编”的部分原因。
The answer to your question is is yes, I'll explain.
Consider common storage types, i.e. size_t, int64_t, etc. These are decided (defined in most cases) at compile time depending on the architecture that you are building on. Don't have them? Fear not, the compiler figured out the underlying meaning of int and how unsigned effects it.
Once the standard C headers figure out the desired word length, everything just adjusts to your system.
Unless, of course, you happen to be cross compiling, then they are decided (or defined) by whatever architecture you specified.
In short, your compiler (well, mostly preprocessor) is going to adjust types to fit the target architecture. Be it the one you are using, or the one you are cross compiling for.
That is if I understand your question correctly. This is one of the few 'magic' abstractions that the language provides and part of the reason why its often called 'portable assembly'.
\\ 它完全依赖于编译器。
或者更正确地说依赖于 C 语言标准 (http://en.wikipedia.org/wiki/C99< /a>)。
正如标准中明确指定的内置类型的大小。
但。它们并不是固定的“一刀切”。
任何架构上的任何编译器都只需要保留最小大小(例如 char 至少为 8 位)。
但它也可以是 16 位甚至 32 位char,具体取决于架构。
并且不同类型之间也保留了相对大小。
这意味着,例如 short 可以是 8、16 或 32 位,但在同一架构上它的长度不能比更宽的类型 int 长。
只是长度更小或相同。
如果编译器开发人员想要制作兼容 C 标准的编译器,那么这是一个内部边界。
\\ It is exclusively compiler dependent.
Or to say more correctly C language standard dependent (http://en.wikipedia.org/wiki/C99).
As in the standard are clearly assigned sizes of bult-in types.
But. They are not fixed "one size for all" there.
There is just a minimal size (e.g. char is at least 8bit) need to be preserved by any compiler on any architecture.
But also it can be 16 or even 32 bit char depend on arch.
And also there is reletive sizes between different types are preserved.
It mean, that for example short is can be 8, 16 or 32 bit, but it cannot be more in length then more wide type int on the same architecture.
Only smaller or same length.
That is a borders inside whith compiler developers must work if they want to make C standard compatible compiler.