What is the cost of sizeof?
I would expect:
sizeof(dynamicArray)
will just return sizeof(pointer)
because in c/c++ dynamic arrays are just pointers.
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 struct
s 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).
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).
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.
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
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.