Main Content

Unused variable

Global variables declared but not used

Description

A non-shared unused global variable has the following properties:

  • The variable is declared in the code.

  • Polyspace® cannot detect a read or write operation on the variable.

In your verification results, these variables are colored gray on the Source, Results List and Variable Access panes. On the Source pane, the coloring is applied to the variable only during declaration. In the Result Details pane, the variable name appears along with the name of the file where it is defined (for extern variables where the definition is not available, ?extern is used for file name.)

Note

The software does not display a complete list of unused global variables. Especially, in C++ projects, unused global variables can be suppressed from display.

Examples

expand all

int var1;
int var2;
int var3;
int var4;

int input(void);

void main() {
    int loc_var = input(), flag=0;

    var1 = loc_var;
    if(0) {
        var3 = loc_var;
    }
    if(flag!=0) {
        var4 =loc_var;
    }


}

If you verify the above code in a C project, the software lists var2, var3 and var4 as non-shared unused variables, and var1 as a non-shared used variable.

var3 and var4 are used in unreachable code and are therefore marked as unused.

Note

In a C++ project, the software does not list the unused variable var2.

Check Information

Language: C | C++