...
Code Block |
---|
const int max = 15;
int a[max]; /* Invalid declaration outside of a function */
const int *p;
/* A const-qualified object can have its address taken. */
p = &max;
|
const
-qualified objects are likely to incur some runtime overhead [Saks 2001b]. Most C compilers, for example, allocate memory for const
-qualified objects. const
-qualified objects declared inside a function body can have automatic storage duration. If so, the compiler will allocate storage for the object, and it will be on the stack. As a result, this storage will need to be allocated and initialized each time the containing function is invoked.
...
Code Block | ||||
---|---|---|---|---|
| ||||
/* ... */ if (age >= 18) { /* Take action. */ } else { /* Take a different action. */ } /* ... */ |
Compliant Solution
...
Code Block | ||||
---|---|---|---|---|
| ||||
enum { ADULT_AGE=18 }; /* ... */ if (age >= ADULT_AGE) { /* Take action. */ } else { /* Take a different action. */ } /* ... */ |
Noncompliant Code Example
...
Recommendation | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
DCL06-C | lowLow | unlikelyUnlikely | mediumMedium | P2 | L3 |
Automated Detection
Tool | Version | Checker | Description | ||||||
---|---|---|---|---|---|---|---|---|---|
|
| Could detect violations of this recommendation merely by searching for the use of "magic numbers" and magic strings in the code itself. That is, any number (except a few canonical numbers: −1, 0, 1, 2) that appears in the code anywhere besides where assigned to a variable is a magic number and should instead be assigned to a | |||||||
| CC2.DCL06 | Fully implemented | |||||||
| 201 S | Fully implemented | |||||||
PRQA QA-C |
| 3120 | Partially implemented |
...