Possible inaccurate example?

Moderators: misra-c, david ward

Post Reply
andream
Posts: 11
Joined: Tue Apr 23, 2013 9:59 am
Company: INTECS
Contact:

Possible inaccurate example?

Post by andream » Thu Nov 17, 2016 5:34 pm

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

andream
Posts: 11
Joined: Tue Apr 23, 2013 9:59 am
Company: INTECS
Contact:

Re: Possible inaccurate example?

Post by andream » Fri Nov 18, 2016 9:59 am

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.

misra-c
Posts: 548
Joined: Thu Jan 05, 2006 1:11 pm

Re: Possible inaccurate example?

Post by misra-c » Fri Dec 09, 2016 9:26 am

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

Post Reply

Return to “8.2 Unused code”