What is the cost of sizeof?

前端 未结 7 1677
一向
一向 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:27

    sizeof(dynamicArray) will just return sizeof(pointer) because in c/c++ dynamic arrays are just pointers.

    0 讨论(0)
  • 2021-02-05 10:27

    In C++, the last case does not exist. sizeof can always be evaluated at compile time, based entirely on the type of the parameter, so the parameter itself is never evaluated.

    If you want something like a dynamic array in C++, you generally use std::vector, in which case there is a run-time cost, but the cost is extremely minor -- O(1). The difference in speed between sizeof and some_vector.size() is rarely relevant. The main one is that since size() isn't a constant, there may be some lost possibility for optimization -- e.g., N/sizeof(short) will usually be optimized to a right shift instead of an actual division, because sizeof(short) is a power of 2. Since the compiler won't normally know that whatever.size() is a power of 2, it has to use an actual division in that case. At the same time, most CPUs optimize division by a power of 2, so the difference remains very small.

    Edit (since the question has been retagged as C): C (since C99) provides both Variable Length Arrays (VLAs) and Flexible Array Members (FAMs). For a variable length array, sizeof does evaluate its parameter, so there is a (minimal) run-time cost -- roughly equivalent to std::vector::size() in C++. For all other types (including structs that include flexible array members), sizeof does not evaluate its operand, so there is no run-time cost (the same as in C++).

    For a struct with a flexible array member: "the size of the structure shall be equal to the offset of the last element of an otherwise identical structure that replaces the flexible array member with an array of unspecified length." (C99, §6.7.2.1/16).

    0 讨论(0)
  • 2021-02-05 10:29

    sizeof only works at compile time. A dynamic array will use a pointer to dynamically allocated memory which will not be included in the result. You'll only get the size of the pointer and housekeeping information (array length, etc).

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 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
    0 讨论(0)
  • 2021-02-05 10:44

    sizeof is a compile time call. It can only work on things that are known at compile time. Your compiler will figure out the size of the structure and substitute a numeric constant.

    0 讨论(0)
提交回复
热议问题