是'sizeof`的操作与评估VLA? [英] Is the operand of `sizeof` evaluated with a VLA?
问题描述
在这个答案促使我问这个问题
在以下code,栏
指向一个可变长度数组,所以的sizeof
确定在运行时,而不是编译时。
In the following code, bar
points to a variable length array, so the sizeof
is determined at runtime instead of compile time.
int foo = 100;
double (*bar)[foo];
该参数是关于是否不使用的sizeof
评估其操作时,操作数是一个可变长度的数组,使得的sizeof(*吧)
未定义行为时栏
未初始化。
The argument was about whether or not using sizeof
evaluates its operand when the operand is a variable length array, making sizeof(*bar)
undefined behavior when bar
is not initialized.
它是不确定的行为,以便使用的sizeof(*吧)
,因为我访问一个未初始化的指针?为中的sizeof
操作数实际计算时的类型是一个可变长度的数组,还是只是确定其类型(如何的sizeof
通常工作)?
Is it undefined behavior to use sizeof(*bar)
because I'm dereferencing an uninitialized pointer? Is the operand of sizeof
actually evaluated when the type is a variable length array, or does it just determine its type (how sizeof
usually works)?
编辑:每个人似乎都引用从C11草案这段话 。有谁知道这是在官本位的措辞?
Everyone seems to be quoting this passage from the C11 draft. Does anyone know if this is the wording in the official standard?
推荐答案
是的,这会导致不确定的行为。
Yes, this causes undefined behaviour.
在N1570 6.5.3.4/2我们有:
In N1570 6.5.3.4/2 we have:
sizeof操作符得到其操作数的大小(以字节为单位),这可能是一个
前pression或类型的括号名称。尺寸是从操作数的类型来确定。结果是整数。 如果操作数的类型是一个可变长度的数组类型,操作数计算;否则,操作数未进行评估,结果是整型常量。
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.
现在我们的问题是:是的类型 *栏
一个可变长度的数组类型
Now we have the question: is the type of *bar
a variable length array type?
由于栏
被声明为指针VLA,提领它应该产生一个VLA。 (但我没有看到具体的文字指定是否它)。
Since bar
is declared as pointer to VLA, dereferencing it should yield a VLA. (But I do not see concrete text specifying whether or not it does).
的注:进一步讨论可以在这里过,或许可以说, *栏
的类型双击[100]
这不是VLA 的
Note: Further discussion could be had here, perhaps it could be argued that *bar
has type double[100]
which is not a VLA.
假如我们认同的类型 *栏
实际上是一个VLA类型,然后在的sizeof *栏
,恩pression *栏
进行评估。
Supposing we agree that the type of *bar
is actually a VLA type, then in sizeof *bar
, the expression *bar
is evaluated.
栏
在这一点上不确定的。现在看看6.3.2.1/1:
bar
is indeterminate at this point. Now looking at 6.3.2.1/1:
如果当它被求值的左值没有指定一个对象,则
行为是未定义
if an lvalue does not designate an object when it is evaluated, the behavior is undefined
由于栏
不指向一个对象(由于是不确定的),评估 *栏
导致未定义行为。
Since bar
does not point to an object (by virtue of being indeterminate), evaluating *bar
causes undefined behaviour.
这篇关于是'sizeof`的操作与评估VLA?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!