what's the mechanism of sizeof() in C/C++? -
It seems that sizeof is not a real work? For example, if you type:
/ P>
int i = 0; Printf ("% d \ n", size (++ i)); Printf ("% d \ n", i);
You may be like the output:
4
and when you gather the code , You will get 'sth like this:
movl $ 4,% esi leaq lc0 (% rip),% rdi xorl% eax,% eax call _printf
Therefore, the compiler has directly placed the "4" as the parameter of print-aided. Then what does the shape do?
You know, why there is a reason; C99, volume 6.5.3.4, §2:
sizeof
operator generates in the size (bytes) of its operand, which can be an expression or parentheses The name can be a type of shape determined by the type of operand the result is an integer if the type of operand is a variable length array type, the operand is evaluated; Otherwise, Operand is not evaluated and its result is an integer constant .
In response to the iPad's comment, here is an example for C99 variable length array case:
#include
The size of foo
is no longer known at compile-time and has to be scheduled at run-time. The generated assembly seems awkward, so do not ask me about the details of the implementation ...
Comments
Post a Comment