未定义的行为。 .. [英] Undefined Behavior. ..

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

问题描述

以下语句是否会调用未定义的行为:


a ^ = b,b ^ = a,a ^ = b;


给出a和b是int型?

要小心,我还没写过^ = b ^ = a ^ = b;当然,这是
undefined。我对前一个陈述感到困惑!


另外,说明声明未定义的原因!

Will the following statement invoke undefined behavior :

a^=b,b^=a,a^=b ;

given that a and b are of int-type ??

Be cautious, I have not written a^=b^=a^=b ; which, of course, is
undefined. I am having some confusion with the former statement!

Also, state the reason for the statement being undefined!

推荐答案

co ************** @ gmail。 com 写道:

以下语句是否会调用未定义的行为:


a ^ = b,b ^ = a,a ^ = b;


假设a和b是int型?
Will the following statement invoke undefined behavior :

a^=b,b^=a,a^=b ;

given that a and b are of int-type ??



否,因为逗号运算符构成序列点

No, because the comma operator constitutes a sequence point


要小心,我还没写过^ = b ^ = a ^ = b;当然,这是
undefined。我对前一个陈述感到困惑!


另外,说明声明未定义的原因!
Be cautious, I have not written a^=b^=a^=b ; which, of course, is
undefined. I am having some confusion with the former statement!

Also, state the reason for the statement being undefined!



6.5.17.1

逗号运算符的左操作数被计算为void表达式;

那里是评估后的一个序列点。


再见,Jojo

6.5.17.1
The left operand of a comma operator is evaluated as a void expression;
there is a sequence point after its evaluation.

Bye, Jojo


<<谨慎,我还没写过^ = b ^ = a ^ = b;当然,这是
undefined。我对前一个陈述感到困惑!>>


我认为这个表达式是有效的,因为赋值运算符被评估

从右到左所以它被解析为

(a ^ =(b ^ =(a ^ = b)))

以上表达式交换两个数字

please如果我错了,请纠正我...
<<Be cautious, I have not written a^=b^=a^=b ; which, of course, is
undefined. I am having some confusion with the former statement!>>

I think this expression is valid as assignment operator is evaluated
from right to left so it is parsed as
(a^=(b^=(a^=b)))
above expression swaps two numbers
please correct me if i am wrong...


c.***********@gmail.com 写道:

<<谨慎,我还没有写a ^ = b ^ = a ^ = b;当然,这是
undefined。我对前一个陈述感到困惑!>>


我认为这个表达式是有效的,因为赋值运算符被评估

从右到左所以它被解析为

(a ^ =(b ^ =(a ^ = b)))

以上表达式交换两个数字

please如果我错了,请纠正我...
<<Be cautious, I have not written a^=b^=a^=b ; which, of course, is
undefined. I am having some confusion with the former statement!>>

I think this expression is valid as assignment operator is evaluated
from right to left so it is parsed as
(a^=(b^=(a^=b)))
above expression swaps two numbers
please correct me if i am wrong...



这是在没有中间序列点的情况下修改''a'两次 - >

未定义的行为


再见,Jojo

This is modifying ''a'' twice without an intermediate sequence point ->
undefined behavoir

Bye, Jojo


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

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