Is the operand of `sizeof` evaluated with a VLA?

后端 未结 3 482
一生所求
一生所求 2020-12-03 07:08

An argument in the comments section of this answer prompted me to ask this question.

In the following code, bar points to a variable length array, so th

3条回答
  •  一个人的身影
    2020-12-03 08:00

    Indeed the Standard seems to imply that behaviour be undefined:

    re-quoting N1570 6.5.3.4/2:

    The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The size is determined from the type of the operand. The result is an integer. If the type of the operand is a variable length array type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an integer constant.

    I think the wording from the Standard is confusing: the operand is evaluated does not mean that *bar will be evaluated. Evaluating *bar does not in any way help compute its size. sizeof(*bar) does need to be computed at run time, but the code generated for this has no need to dereference bar, it will more likely retrieve the size information from a hidden variable holding the result of the size computation at the time of bar's instantiation.

提交回复
热议问题