Main Content

Non-initialized variable

Variable other than local variable is not initialized before being read

Description

This check occurs when you read variables that are not local (global or static variables). It determines whether the variable being read is initialized.

By default, Polyspace® considers that global variables are initialized. The verification checks global variables only if you prevent this default initialization. See also Code Prover Assumptions About Global Variable Initialization.

For more examples of initialization of complex data types, see the equivalent checker for local variables, Non-initialized local variable.

Examples

expand all

int globVar;
int getVal();

void main() {
 int val = getVal();
 if(val>=0 && val<= 100)
   globVar += val;
}

In this example, globVar does not have an initial value when incremented. Therefore, the Non-initialized variable check produces a red error.

The example uses the option to prevent default initialization of global variables.

Correction — Initialize global variable before use

One possible correction is to initialize the global variable globVar before use.

int globVar;
int getVal();

void main() {
 int val = getVal();
 globVar = 0;
 if(val>=0 && val<= 100)
   globVar += val;
}

Check Information

Group: Data flow
Language: C | C++
Acronym: NIV