What is the cost of sizeof?

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

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

Tags:

C++