What is the cost of sizeof?

前端 未结 7 1673
一向
一向 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:31

    sizeof evaluates the size of the type at compile time (evaluating only the type of the expression), so it has no runtime cost (it's exactly as if you put there a constant).

    Since a dynamic array is referred to by a pointer, sizeof will tell you the size of the pointer. In general you must keep track manually of the "real" size of dynamic arrays, there's no supported way to know it from the allocator.

提交回复
热议问题