这是否会导致未定义的行为? [英] Does this cause undefined behaviour?

查看:164
本文介绍了这是否会导致未定义的行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人在另一个论坛上询问如何翻译:

if(x&&(y1 =(y + y1)/ 2))/ *所有带有定义值的整数* /

到另一种语言。


我对自己是否会导致未定义的行为感到困惑


第一名。子表达式(y1 =(y + y1)/ 2)

是否会导致UB,因为/ y /都被修改并使用了没有

插入序列点附加到较大的表达

还是没关系,因为作业的价值是左边

手边是用的?


换句话说,简单的陈述:


y =(y + y1)/ 2;


是明确定义的假设y和y1的合理值,但是当

在一个更大的表达式中使用,如上所述它变成了UB,因为

/ y /现在都被修改和使用了吗?


或,是否可以,因为/ y /实际上并没有在较大的

表达式中使用,但是分配给它的定义明确的值?


我的问题我认为同样适用于......


x = y =(y + y2)/ 2; / *好的,不好,还是坏的比喻? * /


问候,Oz

Someone was asking in another forum how to translate:
if (x && (y1=(y+y1)/2)) /* all integers with defined values */
to another language.

I am at odds with myself as to whether this causes undefined behaviour

in the first place. Does the subexpression (y1=(y+y1)/2)
cause UB because /y/ is both modified and used without an
intervening sequence point attached to the larger expression
or is it ok because the value of the assignment is the left
hand side is used?

In otherwords, the simple statement:

y = (y+y1)/2;

is well defined assuming reasonable values for y and y1 but when
used in a larger expression as above does it become UB since
/y/ is now both modified and used?

Or, is it ok since /y/ isn''t actually used in the larger
expression but the well-defined value assigned to it?

My question would equally apply, I think, to something like...

x = y = (y+y2)/2; /* ok, not ok, or bad analogy? */

Regards, Oz

推荐答案

ozbear< oz ***** @ yahoo。 COM>潦草地写道:
ozbear <oz*****@yahoo.com> scribbled the following:
有人在另一个论坛上询问如何翻译:
if(x&&(y1 =(y + y1)/ 2))/ * all integers with为另一种语言定义值* /

我对自己是否在第一时间导致未定义的行为
感到困惑。子表达式(y1 =(y + y1)/ 2)是否会导致UB,因为/ y /被修改和使用而没有附加到较大表达式的中间序列点
或者是好的,因为赋值的值是左侧
手侧使用?


ITYM / y1 /,而不是/ y /,那里。

换句话说,简单的陈述:
y =(y + y1)/ 2;假设y和y1的值合理,
是明确定义的,但是当如上面的大表达式中使用
时,它会变成UB,因为
/ y /现在都被修改和使用了吗?


不,我不认为它会变成UB,除非你更大的表达

修改或使用/ y1 /(再次,ITYM那个)不同的地方。

或者,是否可以,因为/ y /实际上并没有用于更大的
表达式,而是分配给它的明确定义的值?


是的,我认为就是这种情况。

我认为我的问题同样适用于......
x = y =(y + y2)/ 2; / *好的,不好,还是坏的比喻? * /
Someone was asking in another forum how to translate:
if (x && (y1=(y+y1)/2)) /* all integers with defined values */
to another language. I am at odds with myself as to whether this causes undefined behaviour in the first place. Does the subexpression (y1=(y+y1)/2)
cause UB because /y/ is both modified and used without an
intervening sequence point attached to the larger expression
or is it ok because the value of the assignment is the left
hand side is used?
ITYM /y1/, not /y/, there.
In otherwords, the simple statement: y = (y+y1)/2; is well defined assuming reasonable values for y and y1 but when
used in a larger expression as above does it become UB since
/y/ is now both modified and used?
No, I don''t think it becomes UB, unless your larger expression
modifies or uses /y1/ (again, ITYM that) in a different place.
Or, is it ok since /y/ isn''t actually used in the larger
expression but the well-defined value assigned to it?
Yes, I think that''s the case.
My question would equally apply, I think, to something like... x = y = (y+y2)/2; /* ok, not ok, or bad analogy? */




这是一个很好的类比。那个表达式是,AFAIK,完全安全。

什么会导致UB是y = y =(y + y2)/ 2,修改y两次

没有插入序列点,但你的上面表达

不会那样做。


-

/ - Joona Palaste(pa * ****@cc.helsinki.fi)-------------芬兰-------- \

\-- http://www.helsinki.fi/~palaste -------- -------------规则! -------- /



It''s a good analogy. That expression is, AFAIK, completely safe.
What would cause UB would be y = y = (y+y2)/2, modifying y twice
without intervening sequence points, but your expression above
doesn''t do that.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/


oz ***** @ yahoo.com (ozbear)写道:
oz*****@yahoo.com (ozbear) writes:
有人在另一个论坛上询问如何翻译:
if(x&&(y1 =(y) + y1)/ 2))/ *所有带有定义值的整数* /
到另一种语言。

我和自己在这是否会导致未定义的行为方面存在分歧
第一名。子表达式(y1 =(y + y1)/ 2)是否会导致UB,因为/ y /被修改和使用而没有附加到较大表达式的中间序列点
或者是好的,因为赋值的值是左侧
手侧使用?


不,这个子表达式定义明确,原因与

`x = x + 1''的定义相同。

换句话说,简单的陈述:

y =(y + y1)/ 2;

是明确定义的假设y和y1的合理值但是当
如上所述用于更大的表达式它会变成UB,因为
/ y /现在都被修改和使用了吗?
Someone was asking in another forum how to translate:
if (x && (y1=(y+y1)/2)) /* all integers with defined values */
to another language.

I am at odds with myself as to whether this causes undefined behaviour
in the first place. Does the subexpression (y1=(y+y1)/2)
cause UB because /y/ is both modified and used without an
intervening sequence point attached to the larger expression
or is it ok because the value of the assignment is the left
hand side is used?
No, this subexpression is well-defined, for the same reason that
`x = x + 1'' is well-defined.
In otherwords, the simple statement:

y = (y+y1)/2;

is well defined assuming reasonable values for y and y1 but when
used in a larger expression as above does it become UB since
/y/ is now both modified and used?




不在这种情况下:y(和y1)在较大的

表达式中没有被提及,而不是在子表达式中。此外,

是&&的左右两侧之间的序列点。


-

int main(void){char p [] =" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz。\

\ n",* q =" kl BIcNBFr.NKEzjwCIxNJC" ;; int i = sizeof p / 2; char * strchr(); int putchar(\

); while(* q){i + = strchr(p,* q ++) - p; if(i> =(int)sizeof p)i - = sizeof p-1; putchar(p [i] \

);} return 0;}



Not in this case: y (and y1) aren''t mentioned in the larger
expression other than in that subexpression. Furthermore, there
is a sequence point between the left and right side of &&.

--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}


Joona I Palaste写道:
Joona I Palaste wrote:
ozbear< oz ***** @ yahoo.com>潦草地写道:
ozbear <oz*****@yahoo.com> scribbled the following:
x = y =(y + y2)/ 2; / *好的,不好,还是坏的比喻? * /
x = y = (y+y2)/2; /* ok, not ok, or bad analogy? */



这是一个很好的类比。那个表达式是AFAIK,完全安全。
什么会导致UB是y = y =(y + y2)/ 2,修改y两次
而没有干预序列点,但你的表达式是
不会那样做。


It''s a good analogy. That expression is, AFAIK, completely safe.
What would cause UB would be y = y = (y+y2)/2, modifying y twice
without intervening sequence points, but your expression above
doesn''t do that.




未定义的行为也是由/ using /对象的值产生的(对于

其他用途)而不是确定它的新值)并且还修改它没有插入序列点的
。但在这种情况下,对象只用于确定新值。


所以这显然是未定义的,因为它修改了我两次:


i = a [i ++];


但这也是未定义的,即使它不会修改任何东西

多次:


a [i ++] = i;


-Kevin

-

我的电子邮件地址有效,但会定期更改。

要联系我,请使用最近发布的地址。



Undefined behavior also results from /using/ the value of an object (for
some purpose other than determining its new value) and also modifying it
without an intervening sequence point. But in this case, the object is
only used for determining the new value.

So this is obviously undefined because it modifies i twice:

i = a[i++];

but this is also undefined, even though it does not modify anything
multiple times:

a[i++] = i;

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.


这篇关于这是否会导致未定义的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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