20

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
  • sizeof(someDynamicArray) can not be known at compile time

So how does that last case work?

melpomene
  • 84,125
  • 8
  • 85
  • 148
David
  • 27,652
  • 18
  • 89
  • 138

7 Answers7

35

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

Deep
  • 5,772
  • 2
  • 26
  • 36
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • Question is tagged C++, if it's the same in C++ then would be nice to write it in your answer. – Kashyap Oct 20 '11 at 15:40
  • @thekashyap i retagged the question as C as it's more appropriate. – JaredPar Oct 20 '11 at 15:41
  • 1
    @JaradPar you should have left c++ also. Also, the second half of your statement seems false after reading the other answers. You can't get the size of a dynamic array using sizeof. – David Oct 20 '11 at 15:48
  • 2
    @Dave there is a difference between a variable length array and a dynamic array. You can get the length of a variable length array. http://en.wikipedia.org/wiki/Sizeof – JaredPar Oct 20 '11 at 15:50
  • @JaredPar Oh, interesting. I didn't know that. I'm still waiting on full C99 compliance over here in C++ land. – David Oct 20 '11 at 15:57
  • @Dave I wasn't aware until I read Jerry's answer that variable length arrays were not actually in C++ yet. So odd. But I guess there's not as pressing of a need with `std::vector<>` hanging around – JaredPar Oct 20 '11 at 15:58
  • @Dave: don't wait. It's not going to happen (at least nobody has any plan to make it happen). I'm pretty sure the possibility of adding VLAs to C++ was discussed and rejected. – Jerry Coffin Oct 20 '11 at 15:59
  • @JerryCoffin And here I thought C++11 required full C99 compliance, but upon re-inspection I see they just picked parts of it. – David Oct 20 '11 at 19:57
7

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

Dan F
  • 17,654
  • 5
  • 72
  • 110
6

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).

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
2

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.

Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
0

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

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).

Fred Larson
  • 60,987
  • 18
  • 112
  • 174
0

There is zero runtime cost. In the case of dynamically allocated memory, sizeof gives the size of the static pointer object, not the dynamically allocated memory.

Paul Manta
  • 30,618
  • 31
  • 128
  • 208