Reputation: 41
sizeof
is a compile time operator. See here.
Compilation has many stages. At which stage is the sizeof
operator evaluated?
Upvotes: 2
Views: 387
Reputation: 43558
Typically, after the preprocessor runs and produces the preprocessed translation unit (whole header files pasted in the place of #include
, #define
's substituted all over the place, inactive branches of #ifdef
conditionals completely removed, etc.), the compiler runs. Most modern compilers are usually also able to do the preprocessing themselves, but for historic reasons, the C preprocessor (cpp
) and the C compiler (cc
) are at least conceptually distinct. The output of the former serves as input to the latter.
At consequent stages, it is entirely up to the internal implementation of the compiler what these stages are and what their order is. The most "traditional" pipeline, however, is:
4
in place of the abstract node that represents sizeof(int)
. It would also chew up constant expressions like 3 + 4
into 7
.Note that sizeof
can be evaluated at runtime in case it is applied to a variable-length array (C99 onwards):
int n;
n = ...;
int vl_arr[n];
sizeof(vl_arr); // could be evaluated at runtime if "n" is not known at compile-time
Upvotes: 3