It is preferable to declare immutable values using const, rather than as macro definitions. Using a const declared value means that the compiler is able to check the type of the object, the object has scope, and debugging tools will be able to show the name of the object. For integer constants, it is preferable to use an enum as this guarantees that storage will not be required for the value.
In this example, PI is defined using a macro. In the code, the value is introduced by textual subsitution.
#define PI 3.14159 ... float degrees; float radians; ... radians = degrees*PI/180; |
In this compliant solution, the constant is defined as a const variable.
float const pi = 3.14159; ... float degrees; float radians; ... radians = degrees*PI/180; |
In this example, an integer value is defined using a macro.
#define MAX 42
...
for (int i = 0; i < MAX; ++i) {
...
}
|
This compliant solution uses an enum rather than a macro definition.
int const max = 42;
...
for (int i = 0; i < MAX; ++i) {
...
}
|
Values declared using const cannot be used where compile-time constants are required. So, a const-qualified value cannot be used to specify the
*size of a bit-field member of a structure
*size of an array
*value of an enumeration constant
*value of a case constant.
If any of these are required, then an enum or a macro definition must be used.
Rule |
Severity |
Likelihood |
Remediation Cost |
Priority |
Level |
|---|---|---|---|---|---|
DCL00-A |
1 (low) |
1 (unlikely) |
2 (medium) |
P2 |
L3 |