Page 1 of 1

Possible inaccurate example?

Posted: Thu Nov 17, 2016 5:34 pm
by andream
The following is the example reported for Rule 2.2 (dead code):
In this example, it is assumed that the object pointed to by p is used in other functions.

Code: Select all

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"?

Re: Possible inaccurate example?

Posted: Fri Nov 18, 2016 9:59 am
by andream
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.

Re: Possible inaccurate example?

Posted: Fri Dec 09, 2016 9:26 am
by misra-c
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.