Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Immutable (constant values) should be declared as const-qualified objects (unmodifiable lvalues), enumerations values, or as a last resort, using #define.

In general, it It is preferable to declare immutable values using as const, -qualified objects 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 (certain) debugging tools will be able to can show the name of the object. Const-qualified objects cannot be used where compile-time integer constants are required, namely:

  • 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 integer constant (an rvalue) must be used. For integer constants, it is preferable to use an enum instead of a const-qualified object as this eliminates the possibility of taking the address of the integer constant and does not required that storage is allocated for the value.

Non-Compliant Code Example 1

In this example, PI is defined using a macro. In the code, the value is introduced by textual subsitution.

Code Block
bgColor#FFCCCC
#define PI 3.14159
...
float degrees;
float radians;
...
radians = degrees*PI/180;

Compliant Solution 1

In this compliant solution, the constant is defined as a const variable.

Code Block
bgColor#ccccff
float const pi = 3.14159;
...
float degrees;
float radians;
...
radians = degrees*pi/180;

Non-Compliant Code Example 2

In this example, an integer value is defined using a macroDelcaring immutable integer values as const-qualified objects still allows the programmer to take the address of the object. Also, the constant cannot be used in locations where an an integer constant is required, such as the size of an array.

Code Block
bgColor#FFCCCC
#define MAX 42
...
for (int i = 0; i < max; ++i) {
  ...
}
int const max = 15; 
int const *p; 
int a[max]; /* invalid declaration */
p = &max; /* legal to take the address of a const-qualified object */

Most C compilers will also allocate memory for the const-qualified object.

Compliant Solution 2

This compliant solution uses an enum rather than const-qualified object or a macro definition.

Code Block
bgColor#ccccff
intenum const{ max = 4215 };
...
for (int i 
int a[max]; /* OK */
p = 0&max; i/* < MAX; ++i) {
  ...
}

Exceptions

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.

...

error: '&' on constant */

Risk Assessment

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

DCL00-A

1 (low)

1 (unlikely)

2 (medium)

P2

L3

References

  • ISO/IEC 9899-1999 Sections 6.3.2.1, "Lvalues, arrays, and function designators"; 6.7.2.2, "Enumeration specifiers"; 6.10.3, "Macro replacement"