-Wequence-point警告-这是什么意思,是否违反ANSI C标准? [英] -Wsequence-point warning - what does it mean and does it violate ANSI C standard?

查看:131
本文介绍了-Wequence-point警告-这是什么意思,是否违反ANSI C标准?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在线

tab[i] = tab[i+1] - tab[i] + (tab[i+1] = tab[i]);

我有警告

[Warning] operation on '*(tab + ((sizetype)i + 1u) * 4u)' may be undefined [-Wsequence-point]

我想交换这两个整数数组元素而没有临时变量并且不违反ANSIC.程序仍然可以工作,但是可以吗?

I want to swap these two integer arrays elements without temporary variable and without violation of ANSI C. Program still works, but is it ok?

推荐答案

您的代码依赖于标准未涵盖的行为.因此,您的代码在不同的编译器,同一编译器的不同版本上的行为可能会有所不同,甚至取决于周围的代码,其行为也会有所不同.

Your code relies on behaviour that is not covered by the standard. So your code may behave differently on different compilers, different versions of the same compiler and even behave differently depending on the surrounding code.

问题是您评估并分配给tab[i+1]时没有单独的序列点.对您来说似乎很明显,您的程序将从左到右执行计算,但是C标准不需要这样做.编译器可以将您的代码编译为:

The problem is that you evaluate and assign to tab[i+1] without a separating sequence point. It may seem obvious to you that your program will perform the calculation from left to right, but the C standard does not require this. A compiler could compile your code as:

tab[i+1] = tab[i];
tab[i] = tab[i+1] - tab[i] + tab[i+1];

作为单独的注释.交换两个变量是高度优化且易于识别的操作,如果您编写以下内容,则任何优化的编译器都会选择最快的指令:

As a separate note. Swapping two variables is a highly optimised and easily recognisable operation, any optimising compiler will choose the fastest instructions if you write:

int temp = tab[i];
tab[i] = tab[i+1];
tab[i+1] = temp;

这篇关于-Wequence-point警告-这是什么意思,是否违反ANSI C标准?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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