sizeof的成本是多少? [英] What is the cost of sizeof?

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

问题描述

sizeof的成本是多少?

What is the cost of sizeof?

我希望:

  • sizeof(someclass)可以在编译时知道
  • sizeof(someStaticArray)可以在编译时知道
  • sizeof(someDynamicArray)在编译时可能未知

那最后一种情况如何工作?

So how does that last case work?

推荐答案

C中的 sizeof 构造是一个完全编译时的构造.没有运行时成本.

The sizeof construct in C is a completely compile time construct. There is no runtime cost.

此规则至少有一个例外:可变长度数组.这些数组的大小是在运行时计算的,并且该大小可被应用于它们的任何 sizeof 运算符重用.

There is at least one exception to this rule: variable length arrays. The size of these arrays are computed at runtime and that size is reused for any sizeof operators applied to them.

请注意,可变长度数组与动态数组之间存在差异.C99中添加了可变长度数组,它们确实支持sizeof运算符

Please note there is a difference between a variable length array and a dynamic one. Variable length arrays were added in C99 and they do support the sizeof operator

这篇关于sizeof的成本是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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