简体   繁体   English

“ &&”或“ ||”的右操作数 是可能有副作用的表达

[英]Right hand operand of '&&' or '||' is an expression with possible side effects

Code statement look like below: 代码语句如下所示:

if((temp1 == ID1) || (temp2 == (C_UINT16) ID2))
{

}

I am not sure why QAC is generating this warning. 我不确定QAC为什么会生成此警告。 What will be the side effect & how to avoid this warning. 会有什么副作用以及如何避免此警告。

Details for QAC-help: QAC帮助的详细信息:

The right hand operand of a logical && operator is only evaluated if the left hand operand evaluates to 1 ("true"). 仅当左侧操作数的值为1(“ true”)时,才对逻辑&&运算符的右侧操作数求值。 The right hand operand of a logical || 逻辑||的右操作数 operator is only evaluated if the left hand operand evaluates to 0 ("false"). 仅当左操作数的值为0(“假”)时,才对操作符进行求值。

Because of this behaviour, confusion can arise if the right hand operand of either of these operators generates side effects. 由于这种行为,如果这些运算符之一的右手操作数产生副作用,可能会引起混乱。 Message 3415 is generated to identify such a situation. 生成消息3415以识别这种情况。

Side effects occur when an expression: 当一个表达式出现副作用:

  1. accesses a volatile object 访问一个易失对象
  2. executes an increment, decrement, assignment or compound assignment operation 执行递增,递减,赋值或复合赋值操作
  3. performs I/O or 执行I / O或
  4. calls a function which does any of the above 调用执行上述任何一项功能

However QAC assumes that side effects occur whenever a function is called, unless the function has specifically been identified as being free from side effects by a #pragma statement of the form: 但是,QAC假定每次调用函数时都会发生副作用,除非通过以下形式的#pragma语句将函数明确标识为没有副作用:

#pragma PRQA_NO_SIDE_EFFECTS funcname

While looking at variable temp1 & temp2. 同时查看变量temp1和temp2。 I got to know it is declared as volatile. 我知道它被声明为volatile。 After changing the variable to the normal non-volatile, the warning is not observed. 将变量更改为常规非易失性变量后,未观察到警告。 It was level 4 warning in QAC. 这是QAC中的第4级警告。

Reference: https://cboard.cprogramming.com/c-programming/68666-strange-side-effects.html 参考: https : //cboard.cprogramming.com/c-programming/68666-strange-side-effects.html

Another way to resolve the error to read the volatile variable again before performing the operation again. 解决错误的另一种方法是在再次执行操作之前再次读取volatile变量。

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

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