MISRA C++:2023 Rule 4.6.1
Description
Rule Definition
Operations on a memory location shall be sequenced appropriately.
Rationale
Operations on a memory location are unsequenced if the compiler is permitted to evaluate the operations in any order and to overlap their evaluation. If any of the operations has a side effect, then unsequenced access can result in undefined behavior.
Operations on a memory location are indeterminately sequenced if the compiler is permitted to evaluate the operations in any order, but is not permitted to overlap their evaluation. The output of indeterminately sequenced operations depends on the order of evaluation that the compiler chooses.
To avoid undefined behavior and implementation-defined behavior, use sequenced before operations. The order of evaluation of sequenced before operations is well-defined. See eval_order.
Appropriateness of an operation sequencing can change depending on the C++ standard you use. C++17 changed the evaluation order of certain expressions to sequenced before. For example, C++17 specifies that the evaluation of the right side of an assignment operation is sequenced before the evaluation of the left hand side. The evaluation order of this expression is well-defined in C++17 but unspecified in older C++ standards. This operation sequencing is appropriate and compliant to this rule for C++17, but results in a violation of this rule for older C++ standards.
a[i++] = b[i++]; //Compliant in C++17
Polyspace Implementation
Polyspace® reports a violation of this rule if an expression satisfies any of these conditions:
A variable in the expression is modified more than once or it is both read and written.
An expression allows more than one order of evaluation that can lead to different results.
An expression contains a single
volatile
object that occurs multiple times.An expression contains more than one
volatile
object.
Because volatile
objects can change their value at any
time, an expression containing multiple volatile
objects or multiple
instances of the same volatile
object can produce different results
depending on the order of evaluation.
When checking an expression containing function calls, Polyspace checks the body of the called functions. If the function body contains calls to other functions, their bodies are not checked. For instance:
int f1() {return 1;} int f2() {return g();} int g() {return 2;} void foo(void) { int z = f1() + f2(); }
z = f1()+f2()
,
Polyspace checks the body of f1()
and f2()
,
but does not check the body of g()Troubleshooting
If you expect a rule violation but Polyspace does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.
Examples
Check Information
Group: General Principles |
Category: Required |
Version History
Introduced in R2024b