int类型的sizeof是如何工作的? [英] How does sizeof work for int types?

查看:167
本文介绍了int类型的sizeof是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小程序比较

(1)sizeof,
(2)numeric_limits :: digits,
(3)一个循环的结果

(1) sizeof, (2) numeric_limits::digits, (3) and the results of a loop

,以确保他们都报告关于任何C ++实现的int类型的大小相同的事情。但是因为我不知道sizeof的内部,我不知道是否只是报告numeric_limits ::位数。感谢

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

推荐答案

在大多数编译器上最可能的 sizeof()在其内部类型表中查找给定类型(或对象类型),并将该类型的定义大小的文本插入到它生成的代码中。 这将发生在编译时,而不是运行时。

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.

要回答注释中的问题,没有任何语言定义的访问编译器的内部在C ++中(当然,除了 sizeof()本身)。我知道的唯一类似的语言就是Ada,它提供了 ASIS 用于编写与编译器无关的代码分析工具。

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.

这篇关于int类型的sizeof是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆