编译器如何评估c中的sizeof运算符 [英] how compiler works to evaluate sizeof operator in c

查看:174
本文介绍了编译器如何评估c中的sizeof运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然已经有几个问题在这个论坛&其他相关的sizeof运算符,我不能得到任何答案如何编译器评估sizeof运算符来找到任何数据类型,变量,指针,数组等的大小如果可能还指向我的一些链接,可以帮助我详细了解这。任何帮助将非常感激。感谢。

though there are already several questions asked on this forum & others related to sizeof operator, i could not get any answer on how compiler evaluates the sizeof operator to find the size of any datatype, variable, pointer,array etc. if possible also point me to some links which can help me understand this in detail. any help will be greatly appreciated. thanks.

推荐答案

编译器只是知道原始数据类型的大小;

The compiler just knows the size of primitive datatypes; this knowledge is fundamentally built in to the compiler.

对于传统的固定大小的数组和复杂的数据类型(结构和类),它只是将组件的大小加起来原语和帐户的任何必要的填充。请参见 http://en.wikipedia.org/wiki/Data_structure_alignment

For traditional fixed-size arrays and complex data types (structs and classes), it just adds up the sizes of the constituent primitives and accounts for any necessary padding. See http://en.wikipedia.org/wiki/Data_structure_alignment

sizeof()在大多数情况下是在编译时完成的。异常是可变长度数组(C99中的新),其中它是在运行时计算一旦元素的数量已知。

The sizeof() calculation is done at compile-time in most cases. The exception is for variable-length arrays (new in C99), where it is calculated at runtime once the number of elements is known.

这篇关于编译器如何评估c中的sizeof运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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