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.
Diagnosing This Check
Examples
Non-initialized Variable Used on Right Side of Assignment Operator
#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.
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); }
Non-initialized Variable Used with Relational Operator
#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.
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); }
Non-initialized Variable Passed to Function
#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()
.
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."); }
Non-initialized Array Element
#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.
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]); }
Non-initialized Structure
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.
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); }
Partially Initialized Structure — All Used Fields Initialized
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
andcharacterField
that are used are both initialized.Although the field
doubleField
is not initialized, there is no read or write operation on the fielddoubleField
in the code.
To determine which fields are checked for initialization:
Select the check on the Results List pane or Source pane.
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.
Partially Initialized Structure — Some Used Fields Initialized
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
andcharacterField
that are used are both initialized.The field
doubleField
is not initialized and there is a read operation ondoubleField
in the code.
To determine which fields are checked for initialization:
Select the check on the Results List pane or Source pane.
View the message on the Result Details pane.
Check Information
Group: Data flow |
Language: C | C++ |
Acronym: NIVL |
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)