Does gcc automatically perform a mathematical operations on const values during compilation

3355 views c++

Lets say we perform malloc(4 * sizeof(int)) Now, the number 4 is a constant and from my understanding sizeof is actually compile time function (unless you have a variable inside of it). In this case (considering x86) sizeof(int) would also be 4. My question is, will the gcc optimization perform the calculation itself or will the equation be generated in the asm.

answered question

This is low-hanging fruit for a compiler. The answer is: yes, the compiler will compute this -- it will not be calculated at runtime.

1 Answer


This is called "constant-folding" and yes, it will happen before assembly. Assembly in itself is usually not optimized at all.

posted this

Have an answer?


Please login first before posting an answer.