...
In this noncompliant code example, i
is evaluated more than one in an unsequenced manner, and so the behavior of the expression is undefined:
Code Block | ||||
---|---|---|---|---|
| ||||
void f(int i, const int *b) { int a = i + b[++i]; // ... } |
...
The call to func()
in this noncompliant code example has undefined behavior because the argument expressions are unsequenced:
...
The order of evaluation for function arguments is unspecified. This noncompliant code example exhibits unspecified behavior but not undefined behavior:
Code Block | ||||
---|---|---|---|---|
| ||||
extern void c(int i, int j); int glob; int a() { return glob + 10; } int b() { glob = 42; return glob; } void f() { c(a(), b()); } |
...
Attempting to modify an object in an unsequenced or indeterminately sequenced evaluation may cause that object to take on an unexpected value, which can lead to unexpected program behavior.
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
EXP30EXP50-CPP | Medium | Probable | Medium | P8 | L2 |
...
Tool | Version | Checker | Description | ||||||
---|---|---|---|---|---|---|---|---|---|
|
| Can detect simple violations of this rule. It needs to examine each expression and make sure that no variable is modified twice in the expression. It also must check that no variable is modified once, then read elsewhere, with the single exception that a variable may appear on both the left and right of an assignment operator | |||||||
| EVALUATION_ORDER | Can detect the specific instance where a statement contains multiple side effects on the same value with an undefined evaluation order because, with different compiler flags or different compilers or platforms, the statement may behave differently | |||||||
| CC2.EXP30 | Fully implemented | |||||||
|
| Can detect violations of this rule when the | |||||||
| 35 D | Fully implemented | |||||||
|
|
| |||||||
PRQA QA-C++ |
| 3220, 3221, 3222, 3223 |
...
Bibliography
[ISO/IEC 14882-2014] | 1.9, "Program Execution" |
[MISRA 08] | Rule 5-0-1, "The value of an expression shall be the same under any order of evaluation that the standard permits." |
...