What is the cost of sizeof?

前端 未结 7 1682
一向
一向 2021-02-05 10:06

What is the cost of sizeof?

I would expect:

  • sizeof(someclass) can be known at compile time
  • sizeof(someStaticArray) can be known at compile time
7条回答
  •  太阳男子
    2021-02-05 10:38

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

    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.

    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

    • http://en.wikipedia.org/wiki/Sizeof

提交回复
热议问题