Is Sizeof in C++ Evaluated At Compilation Time or Run Time

Is sizeof in C++ evaluated at compilation time or run time?

sizeof is a compile time operator.

Does sizeof evaluate at compile-time or runtime?

In almost all cases, sizeof is evaluated based on static type information (at compile-time, basically).

One exception (the only one, I think) is in the case of C99's variable-length arrays (VLAs).

How sizeof(array) works at runtime?

sizeof is always computed at compile time in C89. Since C99 and variable length arrays, it is computed at run time when a variable length array is part of the expression in the sizeof operand.

Same for the evaluation of the sizeof operand: it is not evaluated in C89 but in C99 if the operand is of variable length array type it is evaluated. For example:

int n = 5;
sizeof (int [n++]);

// n is now 6

C sizeof calculation run-time, compile-time

In both of these cases the result of sizeof is known at compile time. In the first case it is the size of an int * and in the second case it is the size of an int array of length 10.

The slide seems to think that sizeof in the first case will give you the the amount of memory allocated, however this is not true. The user must keep track of how much space was allocated to ensure that the bounds are not exceeded.

The only time sizeof is calculated at run time is for a variable length array, for example:

int x = foo();
int arr[x];
printf("size=%zu\n", sizeof(arr));

This behavior is dictated by section 6.5.3.4p2 of the C standard:

The sizeof operator yields the size (in bytes) of its
operand, which may be an expression or the parenthesized name of a
type. The size is determined from the type of the operand. The
result is an integer. If the type of the operand is a variable length
array type, the operand is evaluated; otherwise, the operand is not
evaluated and the result is an integer constant.

Does sizeof work at compile time?

sizeof(x) refers to the array which has 5 elements of type int. Hence the output.

The code would be a lot easier to understand if you didn't overload the name x.

Technically what you have here is a variable length array, a VLA. This is because C const actually means read-only and has to be evaluated at run time. Hence sizeof, in this case, is evaluated at runtime.

If you had used a literal to size your array instead, i.e. int x[5]; then sizeof would have been evaluated at compile time.

If the code had been compiled as C++ then the const would be a true const, and so available for evaluation at compile time.

Compile time assertion based on sizeof operator

I mean is it possible that different compilers and different optimizations flags will yield different result?

Yes, this is possible. Your code is not reliable.

If your compiler supports C11, you can use a static assertion instead:

#include <assert.h>
...
static_assert(sizeof(maybeStr[0]) == 1, "parameter must be a string");

Sizeof operator Implementation : How it computes size at compile time?

As you mention, sizeof calculations are normally made at compile time, by the compiler. That's not always true (for example, C99/C11 variable length arrays), but it's a reasonable approximation.

Since the compiler knows (or decides) the size of every type, it can just make a simple constant substitution during compilation.

For your examples, a and n are both variable length arrays, and the sizeof operator is applied at runtime.

From C11, Section 6.5.3.4 The sizeof and _Alignof operators, paragraph 2:

The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The size is determined from the type of the operand. The result is an integer. If the type of the operand is a variable length array type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an integer constant.

Editorial note: sizeof returns a size_t, not an int, so you should use a %zu format specifier instead of %d. Your compiler may warn you if you turn on the appropriate flags.

is sizeof() likely to be optimized away?

sizeof is not a function, it is an operator, and it is evaluated at compile-time before any of your code runs. Therefore, using sizeof incurs zero cost at runtime.



Related Topics



Leave a reply



Submit