Main Content

Global variable not assigned a value in initialization code

Global variable is not assigned a value in the initialization section of program

Description

This check determines if all non-const global variables (and local static variables) are explicitly assigned a value at declaration or in the section of code designated as initialization code.

To indicate the end of initialization code, you enter the line

#pragma polyspace_end_of_init
in the main function. The initialization code starts from the beginning of main and continues up to this pragma. To enable this check, use the option Check that global variables are initialized after warm reboot (-check-globals-init).

The check on a global variable is:

  • Red, if the variable is not initialized at all, either explicitly at declaration or in the initialization code (or is initialized in dead code within the initialization code).

  • Orange, if the variable is not initialized on certain execution paths through the initialization code. For instance, the variable is initialized in an if branch of a conditional statement but not the else branch.

  • Green, if the variable is always initialized once the initialization code completes execution.

In a warm reboot, to save time, the data segment of a program, which might hold variable values from a previous state, is not loaded. Instead, the program is supposed to explicitly initialize all non-const variables before execution. This check verifies that all non-const global variables are indeed initialized in a warm reboot.

Diagnosing This Check

Browse through all instances of the uninitialized or possibly uninitialized variable on the Variable Access pane (or the Global Variables pane in the Polyspace® Access web interface). See if any of the references occur before the pragma polyspace_end_of_init is encountered.

See also Variable Access in Polyspace Desktop User Interface.

Examples

expand all

int aVar;
const int aConst = -1;
int anotherVar;

int main() {
      aVar = aConst;
#pragma polyspace_end_of_init
      return 0;
}

In this example, the global variable aVar is initialized in the initialization code section but the variable anotherVar is not.

int var;
 
int checkSomething(void);
int checkSomethingElse(void);

int main() {
    int local_var;
    if(checkSomething())
    {
        var=0;
    }
    else if(checkSomethingElse()) {
        var=1;
    }
    #pragma polyspace_end_of_init
    var=2;
    local_var = var;
    return 0;
}

The check on var is orange because var might remain uninitialized when the if and else if statements are skipped.

int aVar;
int anotherVar;

int checkSomething();

init0() {
  if (checkSomething())
      aVar = 0;
}
init1() {
  anotherVar = aVar; //Orange check: Non-initialized variable
}
main() {
  init0();
  init1();
#pragma polyspace_end_of_init
}

In this example, both variables aVar and anotherVar appear initialized (green check). However, the following path leads to both variables being non-initialized:

  • The if statement in init0 is skipped, leading to aVar being non-initialized.

  • If aVar is non-initialized, anotherVar is also non-initialized (initialized with unpredictable values).

The issue is highlighted by a different check, Non-initialized variable. The check is orange on this line:

anotherVar = aVar;
Following the orange check, the execution path where aVar is non-initialized is removed from consideration. This removal leads to anotherVar appearing as initialized (green) according to all checks and aVar appearing as initialized (green) according to the check Global variable not assigned a value in initialization code.

To avoid misleading interpretation of green results for initialization:

Check Information

Group: Data flow
Language: C
Acronym: GLOBAL_SET_AT_INITIALIZATION