How does sizeof know the size of the operand array?

前端 未结 12 1221
臣服心动
臣服心动 2020-12-03 13:16

This may be a stupid question but how does the sizeof operator know the size of an array operand when you don\'t pass in the amount of elements in the array. I know it does

12条回答
  •  攒了一身酷
    2020-12-03 14:03

    Sizeof is always evaluated at compile time. In multi pass compiler while generating the symbol table compiler must determine the size of each symbol declared to proceed further to generate intermediate code. So for all sizeof referrences in the code replaces the exact value. At intermediate code generation stage all the operators,statements are converted to right intermediate code (ASM/other format). Finally, m/c code generation stage it converts it to the machine code.

    Some discussions seen above w.r.t the dynamic allocations relating to sizeof is not in the context at all. Any reference to size(*p) where p is a pointer of any data type , compiler just finds out the data type of *p and replaces its size , not go to check the MCB header of the allocated block to see what is the allocated memory size. It is not at run time. For example double *p; sizeof(*p) can still be done without allocating any memory for pointer p. How is it possible?

提交回复
热议问题