Main Content

Used non-shared variable

Global variables used in a single task

Description

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

  • The variable is used only in a single task.

  • Polyspace® detects at least one read or write operation on the variable.

In code that is not intended for multitasking, all global variables are non-shared.

In your verification results, these variables are colored black on the Results List and Variable Access panes.

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.

unsigned int var_1;
unsigned int var_2;
volatile int randomVal;

void task1(void) {
    while(randomVal)
    	operation(1);
}

void task2(void) {
    while(randomVal)
	    operation(2);
}

void operation(int i) {
    if(i==1) {
        var_1++;
    }
    else    {
        var_2++;
    }
}

int main(void) {
    return 0;
}

In this example, even when you specify task1 and task2 for the option Tasks (-entry-points), the software determines that var_1 and var_2 are non-shared.

Even though both task1 and task2 call the function operation, because of the if statement in operation, task1 can operate only on var_1 and task2 only on var_2.

Check Information

Language: C | C++