简体   繁体   English

a =(a + b)-(b = a); C ++与PHP

[英]a = (a + b) - (b = a); C++ vs php

I've been looking around and found formula: a = (a + b) - (b = a) it is supposed to swap two variables (or objects in some cases). 我一直在环顾四周,发现公式: a = (a + b) - (b = a)它应该交换两个变量(或在某些情况下为对象)。 However I tested it with C++ and php, these gave me different result. 但是我用C ++和php进行了测试,这些给了我不同的结果。

php: 的PHP:

$a = 10;
$b = 20;
$a = ($a + $b) - ($b = $a);
echo $a, " ", $b;

This prints 20 10 打印20 10

C++ C ++

int a = 10;
int b = 20;
a = (a + b) - (b = a);
std::cout << a << " " << b;

This prints 10 10 打印10 10

Code looks the same but outputs are different, I've been thinking about two reasons: 代码看起来相同,但输出不同,我一直在思考两个原因:

  1. C++ code is compiling and php is interpreting. C ++代码正在编译,php正在解释。
  2. This formula is useless because it leads to undefined behavior. 此公式无用,因为它导致未定义的行为。

Can somebody explains, why C++ and php output differs in this situation? 有人可以解释一下,为什么在这种情况下C ++和php输出不同吗?

I'm not sure what the rules are in PHP, but in C++, the order of individual sub-expressions isn't strictly defined, or as the technical term is, it is "unspecified" - in other words, the compiler is allowed to calculate b = a before or after it does a + b . 我不确定PHP中的规则是什么,但是在C ++中,未严格定义单个子表达式的顺序,或者按照技术术语,它是“未指定”的-换句话说,允许编译器计算b = a之前或之后的b = a a + b As long as it does a + b and b = a before the subtraction. 只要在减法之前a + bb = a The use of "unspecified" behaviour allows the compiler to produce more efficient code in some cases, or simply that it's possible to build a compiler for some architectures. 使用“未指定”的行为可以使编译器在某些情况下产生更有效的代码,或者仅仅是为某些体系结构构建编译器是可能的。

It also means that if you have an expression that "recalculates" a value within the expression itself, and also using it elsewhere in the expression, you get unedefined behaviour (UB for short). 这也意味着,如果您有一个表达式可以“重新计算”表达式本身内的值,并且在表达式中的其他位置使用它,则会得到未定义的行为(简称UB)。 UB means just that, the behaviour is not defined - almost anything could happen, including what you are seeing and many other alternatives (eg the compiler is allowed to produce 42 as a result as well, even if logic says the answer wouldn't be 42 in this case [it's the wrong question for that!]). UB的意思是,行为没有定义-几乎任何事情都可能发生,包括您看到的内容和许多其他选择(例如,即使逻辑上说答案不是,编译器也可以生成42结果) 42在这种情况下[这是一个错误的问题!])。

I would also suggest that if you want to swap two values, in PHP: 我还建议,如果要交换两个值,请使用PHP:

 $t = $a;
 $a = $b;
 $b = $t;

and in C++: 在C ++中:

 #include <algorithm>

 std::swap(a, b); 

or if you insist on writing your own: 或者如果您坚持要自己编写:

 int t = a;
 a = b;
 b = t; 

Trying to be clever and perform it "without temporary variable" is almost certainly going to make it slower than the use of a temporary - certainly in a compile language like C++ - in a interpreted language like PHP, creating a new variable may add a bit of extra overhead, but it's unlikely to be that big, compared to the extra effort in the logic required. 尝试变得聪明并“没有临时变量”执行它几乎肯定会使其比使用临时变量(肯定是在C ++这样的编译语言中)和在PHP这样的解释语言中使用慢,创建一个新变量可能会增加一点额外的开销,但与所需逻辑上的额外努力相比,它可能不会那么大。

C++ code is completely broken because of undefined behavior. 由于未定义的行为,C ++代码被完全破坏。 (read and write b in one sequence point). (在一个序列点中读取和写入b )。

For PHP: 对于PHP:

$a = 10;
$b = 20;
$a = ($a + $b) - ($b = $a);
//executes like thus
$a = (30) - ($b = $a);
$a = (30) - ($b = $a = 10); //new $a still not computed, using older $a
$a = (30) - (10);
$a = 20;
//then, $a=20 and $b = 10

This is totally related to Operator Precedence , this might be same in C or might not, it depends on precedence if unexpected behavior not occur. 这与操作符优先级完全相关,在C中可能相同,也可能不同,这取决于优先级(如果未发生意外行为)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM