Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rule 12.2: Accessing volatiles in simple assignment statemnt
#6
The example:
Code:
x = v + v;
is strictly-speaking compliant with the rule. There are two read accesses of v, each of which will cause a side-effect, but because the + operator is commutative it doesn't matter in which order the two operands are evaluated in this case.

If the example were:
Code:
x = v - v;
then this would be non-compliant because the value of the expression will depend on the order in which the v operands are evaluated, unless they both evaluate to the same value.

It is also worth noting that:
Code:
x = v1 + v2;        // v1 and v2 both volatile
would be non compliant because the value of v1 might be affected by the evaluation of v2 and vice-versa. Any side-effects arising from the evaluation of v1 might be affected by the evaluation of v2 and vice-versa. The order in which any side-effects arising from evaluation of v1 and v2 would depend on the order in which they were evaluated and this might be observable, for example if v1 and v2 are I/O ports.

Having said all that, the rule does contain the recommendation that volatile objects are accessed only in simple assignments such as:
Code:
x = v;
to ensure that a sequence point occurs between each evaluation of a volatile object. Checking tools are not expected to implement recommendations, but they may do so if they wish. If the recommendation is checked then both of:
Code:
x = x + v;
x = v + v;
are non-compliant
Posted by and on behalf of the MISRA C Working Group
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 2 Guest(s)