标准中的冗余声明? [英] Redundant statement in the Standard?

查看:143
本文介绍了标准中的冗余声明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



最近有人发布了以下有​​关

sizeof运算符的摘录:


6.5.3.4p2:" ; ...如果操作数的类型是变量

长度数组类型,则计算操作数;否则,

操作数未被评估,结果是一个

整数常量。


发生的第一件事对我来说,当你评估一个VLA时,没有任何反应。我的意思是以下应该做什么?


{

int arr [some_runtime_figure];


arr;

}


然后我试着想一下*其他*表达式可能是VLA。我首先想到的是
是一个函数,但以下不会为我编译

使用C99编译器,它说C99不允许返回一个数组来自

a函数:


int Func(void)[5]

{

int arr [5] = {0};


返回arr;

}


int main(void)

{

sizeof(Func());

}


然后我用了更多我的大脑并意识到我在功能签名中硬编码5

所以无论如何我都无法返回VLA




所以我想知道,VLA是什么样的表达,但是b
还有什么东西?将那个小的

段添加到标准中的目的是什么?


将通用的sizeof
没有评估其操作数?


Martin


Someone posted the following excerpt recently in relation to the
sizeof operator:

6.5.3.4p2: "... 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."

The first thing that occured to me was that nothing happens when you
evaluate a VLA. I mean what''s the following supposed to do?

{
int arr[some_runtime_figure];

arr;
}

So then I tried to think what *other* expressions could be a VLA. My
first thought was a function, but the following won''t compile for me
with a C99 compiler, it says C99 doesn''t allow to return an array from
a function:

int Func(void)[5]
{
int arr[5] = {0};

return arr;
}

int main(void)
{
sizeof(Func());
}

Then I used even more of my brain and realised that I hard-coded 5
into the function signature so I wouldn''t be able to return a VLA
anyway.

So I''m left wondering, what kind of expression is a VLA, but which
also DOES SOMETHING? What was the purpose of adding that little
paragraph to the Standard?

Would it not have been simpler to leave us with the universal "sizeof
doesn''t evaluate its operand"?

Martin

推荐答案

Martin Wells< wa **** @ eircom.netwrote:
Martin Wells <wa****@eircom.netwrote:

最近有人发布了以下有​​关
$ b $的摘录b sizeof运算符:


6.5.3.4p2:" ...如果操作数的类型是变量

长度数组类型,则操作数为评估;否则,

操作数未被评估,结果是一个

整数常量。


发生的第一件事对我来说,当你评估一个VLA时,没有任何反应。
Someone posted the following excerpt recently in relation to the
sizeof operator:

6.5.3.4p2: "... 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."

The first thing that occured to me was that nothing happens when you
evaluate a VLA.



考虑一个类型为VLA的表达式,但其评估

涉及副作用。例如,给定


size_t n = some_expression();

char arr [14] [n]

size_t i = 0;


操作


sizeof(arr [i ++])


将返回VLA的大小(应该是n),_和_增量i。

这是不幸的,因为已经声明了n


#define n 93


完全相同的代码会返回_non_-VLA的大小(

wit,93),并且_not_递增i。


道德?永远不要在操作数中包含sideof的副作用,以便最大限度地减少

惊喜。


Richard

Consider an expression whose type is a VLA, but whose evaluation
involves side effects. For example, given

size_t n=some_expression();
char arr[14][n]
size_t i=0;

the operation

sizeof (arr[i++])

would return the size of the VLA (which should be n), _and_ increment i.
This is unfortunate, because had the declaration of n been

#define n 93

the very same code would have returned the size of the _non_-VLA (to
wit, 93), and _not_ incremented i.

The moral? Never include side-effects in the operands to sizeof, so that
surprise is minimised.

Richard


" Richard Bos" <rl*@hoekstra-uitgeverij.nlaécritdansle message de news:
46 ***************** @ news.xs4all.nl ...
"Richard Bos" <rl*@hoekstra-uitgeverij.nla écrit dans le message de news:
46*****************@news.xs4all.nl...

Martin Wells< wa **** @ eircom.netwrote:
Martin Wells <wa****@eircom.netwrote:

>最近有人发布了以下有​​关
sizeof运算符的摘录:

6.5.3.4p2:" ...如果操作数的类型是变量长度数组类型,则计算操作数;否则,
操作数未被评估,结果是一个
整数常量。

我发生的第一件事就是当你没有任何事情发生时
评估VLA。
>Someone posted the following excerpt recently in relation to the
sizeof operator:

6.5.3.4p2: "... 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."

The first thing that occured to me was that nothing happens when you
evaluate a VLA.



考虑一个类型为VLA的表达式,但其评估

涉及副作用。例如,给定


size_t n = some_expression();

char arr [14] [n]

size_t i = 0;


操作


sizeof(arr [i ++])


将返回VLA的大小(应该是n),_和_增量i。

这是不幸的,因为已经声明了n


#define n 93


完全相同的代码会返回_non_-VLA的大小(

wit,93),并且_not_递增i。


道德?永远不要在操作数中包含sizeof的副作用,以便最大限度地减少

惊喜。


Consider an expression whose type is a VLA, but whose evaluation
involves side effects. For example, given

size_t n=some_expression();
char arr[14][n]
size_t i=0;

the operation

sizeof (arr[i++])

would return the size of the VLA (which should be n), _and_ increment i.
This is unfortunate, because had the declaration of n been

#define n 93

the very same code would have returned the size of the _non_-VLA (to
wit, 93), and _not_ incremented i.

The moral? Never include side-effects in the operands to sizeof, so that
surprise is minimised.



实际上道德应该是标准有缺陷。

评估不需要的论点来确定VLA的大小

对象,为什么要强制执行?


相反,如果参数是VLA类型,某种形式的评估将是

必要,但我们必须定义评估

VLA类型的含义。


这方面的一个例子是:


int function();


sizeof(char [function()]);


但是,如果这样的表达式被称为

调用未定义的行为就不会有什么问题。


-

Chqrlie。

Actually the moral should be that the Standard has a defect.
Evaluating the argument in not needed to determine the size of the VLA
object, so why should it be mandated ?

Conversely, if the argument is a VLA-type, some form of evaluation would be
necessary, but we would have to define the meaning of "evaluating a
VLA-type."

An example of this is:

int function();

sizeof(char[function()]);

But then it wouldn''t be much of a problem is such an expression was left as
invoking undefined behaviour.

--
Chqrlie.


Martin Wells< wa **** @ eircom.netwrote:
Martin Wells <wa****@eircom.netwrote:

>最近有人发布了以下关于
sizeof运算符的摘录:

6.5.3.4p2:& ; ...如果操作数的类型是变量长度数组类型,则评估操作数;否则,
操作数未被评估,结果是一个
整数常量。

我发生的第一件事就是当你没有任何事情发生时
评估VLA。
>Someone posted the following excerpt recently in relation to the
sizeof operator:

6.5.3.4p2: "... 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."

The first thing that occured to me was that nothing happens when you
evaluate a VLA.

rl *@hoekstra-uitgeverij.nl (Richard Bos)写道:

rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:


考虑一个类型为VLA的表达式,但其评估值为b
涉及副作用。例如,给定


size_t n = some_expression();

char arr [14] [n]

size_t i = 0;


操作


sizeof(arr [i ++])


将返回VLA的大小(应该是n),_和_增量i。
Consider an expression whose type is a VLA, but whose evaluation
involves side effects. For example, given

size_t n=some_expression();
char arr[14][n]
size_t i=0;

the operation

sizeof (arr[i++])

would return the size of the VLA (which should be n), _and_ increment i.



不,我认为不是这样的。 arr [i ++]不是VLA - 它是

a字符因此sizeof将返回1并且没有任何一方

效果。


我认为有效的例子是sizeof(char [n])。其中n不是

a常数。


-

祝你好运,_ _

。 O操作。 | Serenly Enlightened Majesty of o'',=。/`o

..o |计算机科学,Michalmina86 Nazarewicz(oo)

ooo + - < mina86 * tlen.pl> ---< jid:mina86 * chrome.pl> - ooO - (_) - Ooo--

No, I don''t think that''s the case. arr[i++] is not a VLA -- it''s
a character hence sizeof would return 1 and there will be no side
effects.

I suppose that valid example is "sizeof(char[n])" where n is not
a constant.

--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o'' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>---<jid:mina86*chrome.pl>--ooO--(_)--Ooo--


这篇关于标准中的冗余声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆