sizeof 对于 int 类型如何工作?
我有一个小程序,它比较
(1) sizeof, (2) numeric_limits::数字, (3) 以及循环的结果,
以确保它们在任何 C++ 实现上都报告有关“int 类型”大小的相同内容。然而,因为我不知道 sizeof 的内部结构,所以我不得不怀疑它是否只是报告 numeric_limits::digits 。谢谢
I have a small program which compares
(1) sizeof,
(2) numeric_limits::digits,
(3) and the results of a loop
in an effort to make sure they all report the same thing regarding the size of the "int types" on any C++ implementation. However because I don't know about the internals of sizeof, I have to wonder if it is just reporting numeric_limits::digits. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
大多数编译器上的 sizeof() 很可能会导致编译器在其内部类型表中查找给定类型(或对象的类型),并将该类型定义的大小的文字插入到它生成的代码中。 这将在编译时发生,而不是运行时。
为了回答评论中的问题,C++ 中没有任何语言定义的对编译器内部结构的访问(当然,除了
sizeof()
本身之外)。据我所知,唯一可以让你做类似事情的类似语言是 Ada,它提供了 ASIS用于编写独立于编译器的代码分析工具。Most likely
sizeof()
on most compilers causes the compiler to look the given type (or object's type) up in its internal type table and insert a literal for that type's defined size into the code it generates. This would happen at compile time, not runtime.To answer the question in the comments, there isn't any language-defined access to the compiler's internals in C++ (outside of things like
sizeof()
itself, of course). The only similar language I know of that lets you do stuff like that is Ada, which provides ASIS for writing compiler-independent code analysis tools.sizeof 运算符是一个编译时构造,编译器通过它报告给定类型的实例将在内存中占用的大小(以字节为单位)。
很难给出一个通用的“这就是 sizeof 的工作原理”答案,因为它特定于每个编译器实现。一般来说,它的工作原理是计算类型的每个字段的大小并将它们加在一起,同时考虑对齐。
例如,这是一组合理的输出 [1]
许多编译器将
S2
的大小报告为 12 而不是 9 的原因是它必须考虑对齐问题,因此插入 3 个字节来弥补对于field2
和field3
之间的差距[1] 注意:我说的是合理的,但不能保证:)。 C 编译在大小方面具有很大的灵活性,并且在不知道您正在使用的编译器的情况下几乎不可能说明有关大小的具体信息
The
sizeof
operator is a compile time construct by which the compiler reports the size, in bytes, that an instance of the given type will occupy in memory.It's hard to give a general "this is how sizeof works" answer because it's specific to every compiler implementation. In general though it works by computing the size of every field of a type and adding them together while accounting for alignment.
For example here's a reasonable set of outputs [1]
The reason many compilers will report the size of
S2
as 12 as opposed to 9 is that it must account for alignment issues and hence insert 3 bytes to make up for the gap betweenfield2
andfield3
[1] Note: I said reasonable not guaranteed :). C compiles have a lot of flexibility in sizes and it's nearly impossible to state specifics about sizes without knowing the compiler you're working with
sizeof
的内部原理并不多;它是一个内置运算符,报告其操作数(表达式或类型)的大小(以字节为单位)。您的代码相当复杂 - 使用
typeid
让我想知道...我有一个双语程序(用 C++ 的 C 子集编写),它生成的答案如下:(
这是由 G++ 4.6 生成的。 MacOS X 10.6.7 上的 0(64 位编译)。我使用的代码是:
我不记得为什么我必须用
__STDC_CONSTANT_MACROS
和SPRINT()
与TPRINT()
搞乱,但这似乎正是使代码成为双语所需要的(早在 2010 年 3 月)。There aren't many internals to
sizeof
; it is a built-in operator which reports the size of its operand (either an expression or a type) in bytes.Your code is rather complex - and using
typeid
leaves me wondering...I have a bilingual program (written in the C subset of C++) that produces answers such as:
(This was produced by G++ 4.6.0 on MacOS X 10.6.7 - a 64-bit compilation). The code I used is:
I don't remember exactly why I had to do the messing with
__STDC_CONSTANT_MACROS
andSPRINT()
vsTPRINT()
, but that seemed to be what was needed (back in March 2010) to make the code bilingual.