Start here

Home
About Klocwork
What's new
Fixed issues
Release notes
Installation

Reference

C/C++ checkers
Java checkers
C# checkers
MISRA C 2004 checkers
MISRA C++ 2008 checkers
MISRA C 2012 checkers
MISRA C 2012 checkers with Amendment 1
Commands
Metrics
Troubleshooting
Reference

Product components

C/C++ Integration build analysis
Java Integration build analysis
Desktop analysis
Refactoring
Klocwork Static Code Analysis
Klocwork Code Review
Structure101
Tuning
Custom checkers

Coding environments

Visual Studio
Eclipse for C/C++
Eclipse for Java
IntelliJ IDEA
Other

Administration

Project configuration
Build configuration
Administration
Analysis performance
Server performance
Security/permissions
Licensing
Klocwork Static Code Analysis Web API
Klocwork Code Review Web API

Community

View help online
Visit RogueWave.com
Klocwork Support
Rogue Wave Videos

Legal

Legal information

MISRA.ENUM.INIT

Non-first enumerator is explicitly initialized, but not all elements are explicitly initialized.

MISRA-C Rule 9.3 (required): In an enumerator list, the "=" construct shall not be used to explicitly initialise members other than the first, unless all items are explicitly initialised.

If an enumerator list is given with no explicit initialisation of members, then C allocates a sequence of integers starting at 0 for the first element and increasing by 1 for each subsequent element.

An explicit initialisation of the first element, as permitted by the above rule, forces the allocation of integers to start at the given value. When adopting this approach, it is essential to ensure that the initialisation value used is small enough that no subsequent value in the list will exceed the int storage used by enumeration constants.

Explicit initialisation of all items in the list, which is also permissible, prevents the mixing of automatic and manual allocation, which is error prone. However, it is then the responsibility of the programmer to ensure that all values are in the required range, and that values are not unintentionally duplicated.

Example

enum colour { red=3, blue, green, yellow=5 };   /* non compliant      */
   /* green and yellow represent the same value - this is duplication */
enum colour { red=3, blue=4, green=5, yellow=5 };   /* compliant      */
   /* green and yellow represent the same value - this is duplication */

MISRA-C++ Rule 8-5-3 (required): In an enumerator list, the = construct shall not be used to explicitly initialize members other than the first, unless all items are explicitly initialized.

Rationale

If an enumerator list is given with no explicit initialization of members, then C++ allocates a sequence of integers starting at zero for the first element and increasing by one for each subsequent element.

An explicit initialization of the first element, as permitted by the above rule, forces the allocation of integers to start at the given value. When adopting this approach it is essential to ensure that the initialization value used is small enough that no subsequent value in the list will exceed the int storage used by enumeration constants.

Explicit initialization of all items in the list, which is also permissible, prevents the mixing of automatic and manual allocation, which is error prone. However it is then the responsibility of the developer to ensure that all values are in the required range, and that values are not unintentionally duplicated.

Example

The following example assigns the same value to the 'green' and 'yellow' enumeration constants. It is unclear to a reviewer if this was intentional or an error.

enum colour { red=3, blue, green, yellow=5 }; // Non-compliant

However, if all the items are explicitly initialized, then the duplicated values are acceptable as the duplication is readily detectable by anyone reviewing the code.

enum colour { red=3, blue=4, green=5, yellow=5 }; // Compliant