Main Content

Non-initialized local variable

Local variable is not initialized before being read

Description

This check occurs for every local variable read. It determines whether the variable being read is initialized.

Examples

expand all

#include <stdio.h>
    
void main(void) {
   int sum;
   for(int i=1;i <= 10; i++)
      sum+=i;
   printf("The sum of the first 10 natural numbers is %d.", sum);
 }

The statement sum+=i; is the shorthand for sum=sum+i;. Because sum is used on the right side of an expression before being initialized, the Non-initialized local variable check returns a red error.

Correction — Initialize variable before using on right side of assignment

One possible correction is to initialize sum before the for loop.

#include <stdio.h>
    
void main(void) {
   int sum=0;
   for(int i=1;i <= 10; i++)
      sum+=i;
   printf("The sum of the first 10 natural numbers is %d.", sum);
 }
#include <stdio.h>
    
int getTerm();
    
void main(void) {
    int count,sum=0,term;
    
    while( count <= 10  && sum <1000) {
       count++;
       term = getTerm();
       if(term > 0 && term <= 1000) sum += term;
      }
    
    printf("The sum of 10 terms is %d.", sum);
 }

In this example, the variable count is not initialized before the comparison count <= 10. Therefore, the Non-initialized local variable check returns a red error.

Correction — Initialize variable before using with relational operator

One possible correction is to initialize count before the comparison count <= 10.

#include <stdio.h>
    
int getTerm();
    
void main(void) {
    int count=1,sum=0,term;
    
    while( count <= 10  && sum <1000) {
       count++;
       term = getTerm();
       if(term > 0 && term <= 1000) sum+= term;
      }
    
    printf("The sum of 10 terms is %d.", sum);
 }
#include <stdio.h>

int getShift();
int shift(int var) {
    int shiftVal = getShift();
    if(shiftVal > 0 && shiftVal < 1000)
        return(var+shiftVal);
    return 1000;
}
    
void main(void) {
    int initVal;
    printf("The result of a shift is %d",shift(initVal));
}

In this example, initVal is not initialized when it is passed to shift(). Therefore, the Non-initialized local variable check returns a red error. Because of the red error, Polyspace® does not verify the operations in shift().

Correction — Initialize variable before passing to function

One possible correction is to initialize initVal before passing to shift(). initVal can be initialized through an input function. To avoid an overflow, the value returned from the input function must be within bounds.

#include <stdio.h>

int getShift();
int getInit();
int shift(int var) {
    int shiftVal = getShift();
    if(shiftVal > 0 && shiftVal < 1000)
        return(var+shiftVal);
    return 1000;
}
    
void main(void) {
   int initVal=getInit();
   if(initVal >0 && initVal < 1000)
     printf("The result of a shift is %d",shift(initVal));
   else
     printf("Value must be between 0 and 1000.");
 }
#include <stdio.h>
#define arrSize 19
 
void main(void)
{
  int arr[arrSize],indexFront, indexBack;
  for(indexFront = 0,indexBack = arrSize - 1; 
    indexFront < arrSize/2; 
    indexFront++, indexBack--) {
    arr[indexFront] = indexFront;
    arr[indexBack] = arrSize - indexBack - 1;
  }
  printf("The array elements are: \n");
  for(indexFront = 0; indexFront < arrSize; indexFront++)
    printf("Element[%d]: %d", indexFront, arr[indexFront]);
 }

In this example, in the first for loop:

  • indexFront runs from 0 to 8.

  • indexBack runs from 18 to 10.

Therefore, arr[9] is not initialized. In the second for loop, when arr[9] is passed to printf, the Non-initialized local variable check returns an error. The error is orange because the check returns an error only in one of the loop runs.

Due to the orange error in one of the loop runs, a red Non-terminating loop error appears on the second for loop.

Correction — Initialize variable before passing to function

One possible correction is to keep the first for loop intact and initialize arr[9] outside the for loop.

#include <stdio.h>
#define arrSize 19
 
void main(void)
{
  int arr[arrSize],indexFront, indexBack;
  for(indexFront = 0,indexBack = arrSize - 1;
    indexFront < arrSize/2; 
    indexFront++, indexBack--) {
    arr[indexFront] = indexFront;
    arr[indexBack] = arrSize - indexBack - 1;
  }
  arr[indexFront] = indexFront;
  printf("The array elements are: \n");
  for(indexFront = 0; indexFront < arrSize; indexFront++)
    printf("Element[%d]: %d", indexFront, arr[indexFront]);
}
typedef struct S { 
   int integerField; 
   char characterField;
}S;

void operateOnStructure(S);
void operateOnStructureField(int);

void main() {
  S myStruct;
  operateOnStructure(myStruct);
  operateOnStructureField(myStruct.integerField);
}

In this example, the structure myStruct is not initialized. Therefore, when the structure myStruct is passed to the function operateOnStructure, a Non-initialized local variable check on the structure appears red.

Correction— Initialize structure

One possible correction is to initialize the structure myStruct before passing it to a function.

typedef struct S { 
   int integerField; 
   char characterField;
}S;

void operateOnStructure(S);
void operateOnStructureField(int);

void main() {
  S myStruct = {0,' '};
  operateOnStructure(myStruct);
  operateOnStructureField(myStruct.integerField);
}
typedef struct S { 
   int integerField; 
   char characterField;
   double doubleField;
}S;

int getIntegerField(void);
char getCharacterField(void);

void printIntegerField(int);
void printCharacterField(char);

void printFields(S s) {
 printIntegerField(s.integerField);
 printCharacterField(s.characterField);
}

void main() {
  S myStruct;
  
  myStruct.integerField = getIntegerField();
  myStruct.characterField = getCharacterField();
  printFields(myStruct);
}

In this example, the Non-initialized local variable check on myStruct is green because:

  • The fields integerField and characterField that are used are both initialized.

  • Although the field doubleField is not initialized, there is no read or write operation on the field doubleField in the code.

To determine which fields are checked for initialization:

  1. Select the check on the Results List pane or Source pane.

  2. View the message on the Result Details pane.

Note that in the special case where none of the fields are used, the checks for initialization are orange instead of green if all the fields.are uninitialized.

typedef struct S { 
   int integerField; 
   char characterField;
   double doubleField;
}S;

int getIntegerField(void);
char getCharacterField(void);

void printIntegerField(int);
void printCharacterField(char);
void printDoubleField(double);

void printFields(S s) {
 printIntegerField(s.integerField);
 printCharacterField(s.characterField);
 printDoubleField(s.doubleField);
}

void main() {
  S myStruct;
  
  myStruct.integerField = getIntegerField();
  myStruct.characterField = getCharacterField();
  printFields(myStruct);
}

In this example, the Non-initialized local variable check on myStruct is orange because:

  • The fields integerField and characterField that are used are both initialized.

  • The field doubleField is not initialized and there is a read operation on doubleField in the code.

To determine which fields are checked for initialization:

  1. Select the check on the Results List pane or Source pane.

  2. View the message on the Result Details pane.

Check Information

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