About the power of the sizeof operator in C ++

I am reading Modern C ++ design. The name sizeof was mentioned in the following description. The following paragraph is explained in terms of general programming.

There is an amazing amount of power in sizeof: you can apply sizeof to any expression, no matter how complicated, and sizeof returns its size without evaluating that expression at run time. This means that sizeof knows about overloading, creating templates, conversion rules - everything that can take part in a C ++ expression. In fact, sizeof hides the full facility for inferring an expression type; eventually sizeof throws the expression and returns only the size of its result.

My question is that the author means that sizeof returns its size with the actual evalutating exression at runtime. And also in the last line it was mentioned that sizeof throws out the expression. Ask for help in understanding these statements; it would be nice if this were done with an example.

thanks

+4
source share
5 answers

what the author does means that sizeof returns its size without actually evalutating exression at run time.

This means that sizeof(1/0) will give sizeof(int) , although 1/0 usually interrupts the program because dividing by zero is a run-time error. Also, for any p declared as T* p , sizeof(*p) will give sizeof(T) no matter what value is stored in p , even if p hangs or is not initialized at all.

+2
source

sizeof is evaluated at compile time: the compiler evaluates the type of expression following the sizeof operator. This is done once and for all by the compiler, so the sentence is "without actually evaluating this expression at runtime."

The compiler calculates the type, then it can infer the size of the expression from this type, and then, even during compilation, the whole sizeof expression is replaced by the calculated size. Thus, the expression itself is not part of the executable code. This means that the sentence "sizeof discards the expression and returns only the size of its result."

+2
source

The following is a sizeof value of type i++ that has an int value (usually an int has 4 or 8 bytes, so it will probably give you a 4 or 8 value). However, since the expression is not evaluated, the runtime action is not performed for the expression.

 int i = 0; sizeof(i++); 

Evaluating an expression basically means performing its side effects (for example, incrementing a variable) or reading values ​​from memory or registers at run time. Thus, in a sense, sizeof β€œdiscards” its operand, since it really does not perform the execution operation it performs (the value of i will still be zero).

+1
source

The compiler must calculate the sizes of types / structures / classes for various operations. The sizeof operator makes these sizes available to your program as a constant. For example, if you execute sizeof(int) , the compiler knows how large the int (in bytes) is and inserts this value instead. The same applies to more complex things like sizeof(myVariable) with myVariable of type MyClass : the compiler knows how much space MyClass occupies and, therefore, can insert this value.

The fact is that this evaluation is performed at compile time: the result is a number. The evaluation does not need to be repeated at run time.

0
source

This means that int j=sizeof(int); will be compiled to int j=4;

I read the compiled assembly, the calculation is not actually performed at runtime!

-1
source

Source: https://habr.com/ru/post/1341416/


All Articles