How sizeof(array) works at runtime?

前端 未结 6 1780
心在旅途
心在旅途 2020-11-28 10:45

I have read that sizeof operator in C is interpreted at compile time and since at compile time compiler knows the array size and its type,sizeof is abled to compute the numb

6条回答
  •  一个人的身影
    2020-11-28 10:56

    Regardless of whether sizeof is computed at compile time or runtime (or more formally speaking, whether its result is an integer constant expression), the result of sizeof is purely based on the type of its argument and not any hidden data accompanying the variable-length array itself. Of course when sizeof is applied to a variably-modified type, the generated program must keep track of that size somewhere. But it might simply recompute it if the expression was simple enough and the variables it originally derived the length from cannot have changed. Or, it could store the size of the type somewhere (essentially in a hidden local variable), but this would not be connected to the VLA object in any observable way (for example, if you pass a pointer to the first element of the VLA to another function, that pointer cannot be used to recover the VLA length).

提交回复
热议问题