Wiki Markup |
---|
Immutable objects should be {{const}}\-qualified. Enforcing invariants such as object immutability using existing {{const}}\-qualification helps ensures the correctness and security of applications. ISO/IEC PDTR 24772 \[[ISO/IEC PDTR 24772|AA. C References#ISO/IEC PDTR 24772]\], for example, recommends labeling parameters as constant to avoid the unintentional modification of function arguments. |
Immutable (constant) values should be declared as const
-qualified objects (unmodifiable lvalues), macro definitions using #define
, or enumeration constants.
The C programming language has several mechanisms for creating symbolic constants: const
-qualified objects, enumeration constants, and macro definitions.
Objects that are const
-qualified have scope and can be type-checked by the compiler. Because these are named objects (unlike macro definitions), (certain) debugging tools can show the name of the object. The objects also consumes memory (though this is not too important). Unfortunately, const
-qualified objects cannot be used where compile-time integer constants are required, namely to define the
- size of a bit-field member of a structure
- size of an array (except in the case of variable length arrays)
- value of an enumeration constant
- value of a
case
constant
An enumeration constant is a member of an enumeration. Enumeration constant can be used to represent an integer constant expression that has a value representable as an int
. Unlike const
-qualified objects, enumeration constants do not require that storage is allocated for the value so it is not possible to take the address of an enumeration constant.
#define:
- operates at compile time
- consumes no memory (though this is not too important)
- can use in compile-time constant expression
- uses different syntax; can make mistake with ;
- can't create pointers to
- no type checking
const:
- operates at run time
- consumes memory (though this is not too important)
- can't use in compile-time constant expression
- uses consistent syntax
- can create pointers to
- does type checking
If any of these are required, then an integer constant (which would be an rvalue) must be used.
Method | Evaluated at | Consumes Memory | Viewable by Debuggers | Type Checking | Compile-time constant expression |
---|---|---|---|---|---|
Enumerations | compile time | no | yes | yes | no |
| run time | yes | yes | yes | no |
Macros | preprocessor | no | no | no | yes |
This recommendation is related to EXP07-A. Do not diminish the benefits of constants by assuming their values in expressions.
Non-Compliant Code Example (Object-Like Macro)
A preprocessing directive of the form:
#
define
identifier replacement-list
...
STR05-A. Prefer making string literals const-qualified describes a specialized case of this recommendation.
Non-Compliant Code Example
In this non-compliant code example, PI
pi
is defined as an object-like macro. Following the definition, each subsequent occurrence of the string "PI" is replaced by the string "3.14159f" by textual substitution.declared as a float
. Although pi is a mathematical constant, its value is not protected from accidental modification.
Code Block | ||
---|---|---|
| ||
#definefloat pi PI= 3.14159f; float degrees; float radians; /* ... */ radians = degrees * PI / 180; |
...
In this compliant solution, pi
is declared as a const
-qualified object, allowing the constant to have scope and to have its value inspected by a debugger.
Code Block | ||
---|---|---|
| ||
const float pi = 3.14159f; float degrees; float radians; /* ... */ radians = degrees * pi / 180; |
...
Non-Compliant Code Example (Immutable Integer Values)
...
Code Block | ||
---|---|---|
| ||
enum { max = 15 };
int a[max]; /* OK */
const int *p;
p = &max; /* error: '&' on constant */
|
Exceptions
DCL00-EX1 Constant values that may be passed as compile-time arguments must be macro definitions, as shown by this example:
Code Block |
---|
#ifndef MYPORTNUMBER /* might be passed on compile line */ # define MYPORTNUMBER 1234 #endif |
Risk Assessment
Using ordinary variables to hold constants instead of using enumeration constants or const
-qualified objects can result in a value intended to be constant being changed at runtime.
...