Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
10.5 Iterating over an enum
#2
Iterating over an enumeration is non-compliant with MISRA C:2012, because in general it is not known that all values in the iteration loop exist as enumeration values. For example
Code:
typedef enum { EA =1, EB = 3, ELAST = 5 } en1;
An alternative way of writing the loop would be
Code:
for ( adc_t en = ADC_VBAT; en < ADC_NUM_SIGNALS; en++ )
  {
    AdcClearAverage( en ) ;
  }
but this does violate rule 10.1 as ++ is not allowed on objects with an enumeration type.

Both the original and the alternative way of writing the loop would require a deviation. The deviation would need to include the information that
  • 1. The enumeration type contained no gaps in the numbering sequence
    2. The start and end points of the loop were members of the enumeration type
In addition you might also want to state that the start/end points of the loop are the first and last members of the enumeration type.
Posted by and on behalf of the MISRA C Working Group
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 1 Guest(s)