如何在不改变逻辑的情况下删除警告? [英] How to remove warning without changing the logic ?

查看:98
本文介绍了如何在不改变逻辑的情况下删除警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

# include <stdio.h>
int main()
{
	int a,b;
	scanf("%d%d",&a,&b);
	b=(a+b)-(a=b);
	printf("after swapping: a is %d, b is %d", a,b);
	return 1;
}







警告:'a'上的操作可能未定义[-Wsequence-点数]



任何人都可以在不改变逻辑的情况下提供解决方案而无需警告。




warning: operation on 'a' may be undefined [-Wsequence-point]

Can anyone provide solution without warning without changing logic.

推荐答案

可以任何人都可以在没有改变逻辑的情况下提供解决方案。

编号

因为你正在做的是使用副作用,计算结果是undefined - 它们取决于编译器对执行顺序的作用。而且,如果你在x - y中首先评估x或y,那么正常数学并不重要,编译器可以自由地计算出y,然后才算出x。如果确实如此,则最终完成的计算是b = b,因为先执行(a = b),然后执行(a + b),然后执行减法。



它也变得更糟:编译器可以自由地改变优化后发生的事情:所以在开发中起作用的东西可能在生产中不起作用......



C规范允许编译器编写者完全自由,但是其他类似的语言要么将执行顺序定义为从左到右或从右到左,这意味着即使这个代码也不能被信任也可以工作。代码是相同的!



不要做这样的事情:它们难以阅读,依赖编译器,不可靠,并且很难维护。
"Can anyone provide solution without warning without changing logic."
No.
Because what you are doing is using a side effect, and the results of the calculation are undefined - they depend on what the compiler does with the execution order. And since with "normal" math is doesn't matter if you evaluate x or y first in "x - y" the compiler is at liberty to work out "y" before it works out "x". If it does, then the calculation that eventually gets done is b = b because (a = b) is executed first, then (a + b), then the subtraction.

It gets worse as well: the compiler is at liberty to change what occurs as a result of optimisations: so what works in development may not work in production...

The C specification allows the compiler writer complete freedom, but other similar languages do either define the execution order as left-to-right or right-to-left which means that this code can't be trusted to work the same even if the code is identical!

Don't do things like this: they are hard to read, compiler dependant, unreliable, and very difficult to maintain.


正如格里夫所说:不要用常规代码做这样的游戏!它们很难阅读和维护。你的例子只是偶然的,并且依赖于编译器。



只是为了解决这个难题的乐趣:这是一个可以工作并适合一行的版本C代码:

As Griff said: Don't do such games in regular code! They are hard to read and maintain. And your example only works by chance and is compiler dependent.

Just for the fun of solving the puzzle: Here is a version that does work and fits in a single line of C-code:
a = a^b, b=a^b, a=a^b;



这不依赖于编译器。但仍然没有人会猜到这条线应该做什么。所以,我绝不会在生产代码中使用那个小技巧​​,而是使用临时变量。顺便说一句,在许多情况下,编译器很可能会将临时文件放入寄存器中,然后使用三个简单的寄存器副本而不是XOR。 (但我从来没有做过时间。这不值得努力。)



在C ++中,你当然只需使用


That is not compiler dependent. But still nobody would normally guess what this line is supposed to do. So, I would never use that little "trick" in production code, but use a temporary variable instead. That by the way might also be slightly faster in many cases as the compiler will most likely put the temporary in a register and then use three simple register copies instead of XORs. (I have never done timings on that, though. It isn't worth the effort.)

In C++ you would of course simply use

std::swap (a, b);


这篇关于如何在不改变逻辑的情况下删除警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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