Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Possible inaccurate example?
#1
The following is the example reported for Rule 2.2 (dead code):

Quote:In this example, it is assumed that the object pointed to by p is used in other functions.

Code:
extern volatile uint16_t v;
extern char *p;

void f (void )  {
   uint16_t x;
   (void) v;  /* Compliant - v is accessed for its side effect * and the cast  to void is permitted by exception */
   (int32_t) v; /* Non-compliant - the cast operator is dead */
   v >> 3;     /* Non-compliant - the >> operator is dead */
   x = 3;      /* Non-compliant - the = operator is dead * - x is not subsequently read */
   *p++;       /* Non-compliant - result of * operator is not used */
   (*p)++;     /* Compliant - *p is incremented */
}

I'm not sure that last but one statement represents a non-compliance. In fact, the expression *p++ is treated as *(p++), as the precedence of postfix ++ is higher than *. Indeed the result of * operator is not used, but p value results permanently increased, and p is a global variable. How can be stated that it is not compliant, alias dead code, i.e. "...whose removal would not affect program behaviour"?
<t></t>
#2
Sorry for viewers. I apologize, I simpy didn't notice that the declared non compliance, in the cited example, is not dead code, rather unused "*" operator.
<t></t>
#3
Rule 2.2 applies to each operator in an expression, not the expression as a whole.

As was mentioned in the 2nd post, the "*" operation is unused and constitues dead code. The expression "*p++;" could be replaced by "p++;" with no change in behaviour of the program.
Posted by and on behalf of the MISRA C Working Group


Forum Jump:


Users browsing this thread: 2 Guest(s)