Main Content

Unmodified variable not const-qualified

Variable not const-qualified but variable value not modified during lifetime

Since R2020a

Description

This defect occurs when a local variable is not const-qualified and one of the following statements is true during the variable lifetime:

  • You do not perform write operations on the variable after initialization.

  • When you perform write operations, you reassign the same constant value to the variable.

The checker considers a variable as modified if its address is assigned to a pointer or reference (unless it is a pointer or reference to a const variable), passed to another function, or otherwise used. In these situations, the checker does not suggest adding a const qualifier.

The checker flags arrays as candidates for const-qualification only if you do not perform write operations on the array elements at all after initialization.

Risk

const-qualifying a variable avoids unintended modification of the variable during later code maintenance. The const qualifier also indicates to a developer that the variable retains its initial value in the remainder of the code.

Fix

If you do not expect to modify a variable value during its lifetime, add the const qualifier to the variable declaration and initialize the variable at declaration.

If you expect the variable to be modified, see if the absence of a modification indicates a programming omission and fix the issue.

Examples

expand all

#include <string.h>

char returnNthCharacter (int n) {
    char* pwd = "aXeWdf10fg" ;
    char nthCharacter;
        
    for(int i=0; i < strlen(pwd); i++) {
        if(i==n)
            nthCharacter = pwd[i];
    }
    return nthCharacter;
}

In this example, the pointer pwd is not const-qualified. However, beyond initialization with a constant, it is not reassigned anywhere in the returnNthCharacter function.

Correction – Add const at Variable Declaration

If the variable is not intended to be modified, add the const qualifier at declaration. In this example, both the pointer and the pointed variable are not modified. Add a const qualifier to both the pointer and the pointed variable. Later modifications cannot reassign the pointer pwd to point at a different variable nor modify the value at the pointed location.

#include <string.h>

char returnNthCharacter (int n) {
    const char* const pwd = "aXeWdf10fg" ;
    char nthCharacter;
        
    for(int i=0; i < strlen(pwd); i++) {
        if(i==n)
            nthCharacter = pwd[i];
    }
    return nthCharacter;
}

Note that the checker only flags the missing const from the pointer declaration. The checker does not determine if the pointed location also merits a const qualifier.

void resetBuffer(int aCondition) {
    int addr = 0xff;
    if(aCondition){
        addr = 0xff;
    }
    else {
        addr = 0xff;
    }
}

In this example, the variable addr is initialized to a value and reassigned the same value twice. In larger code examples, such issues can easily arise from copy-paste errors.

Correction – Fix Programming Error

The reassignment in this example indicates a possible programming error. One possible correction is to fix the programming error and thereby avoid reassigning the same value.

void resetBuffer(int aCondition) {
    int addr = 0xff;
    if(aCondition){
        addr = 0x00;
    }
}

Result Information

Group: Good practice
Language: C | C++
Default: Off
Command-Line Syntax: UNMODIFIED_VAR_NOT_CONST
Impact: Low

Version History

Introduced in R2020a

expand all