IssueImproper array initialization occurs
when Polyspace®
Bug Finder™ considers that an array initialization
using initializers is incorrect.
This defect applies to normal and designated initializers. In
C99, with designated initializers, you can place the elements of an
array initializer in any order and implicitly
initialize some array elements. The designated initializers use the
array index to establish correspondence between an array element and
an array initializer element. For instance, the statement int
arr[6] = { [4] = 29, [2] = 15 }
is equivalent to int
arr[6] = { 0, 0, 15, 0, 29, 0 }
.
You can use initializers incorrectly in one of the following
ways.
Issue | Risk | Possible Fix |
---|
In your initializer for a one-dimensional array, you
have more elements than the array size. | Unused array initializer elements indicate a possible
coding error. | Increase the array size or remove excess elements. |
You place the braces enclosing initializer values incorrectly. | Because of the incorrect placement of braces, some array
initializer elements are not used. Unused array initializer
elements indicate a possible coding error. | Place braces correctly. |
In your designated initializer, you do not initialize
the first element of the array explicitly. | The implicit initialization of the first array element
indicates a possible coding error. You possibly overlooked the fact
that array indexing starts from 0. | Initialize all elements explicitly. |
In your designated initializer, you initialize an element
twice. | The first initialization is overridden. The
redundant first initialization indicates a possible coding error. | Remove the redundant initialization. |
You use designated and nondesignated initializers in
the same initialization. | You or another reviewer of your code cannot determine
the size of the array by inspection. | Use either designated or nondesignated initializers. |
FixThe fix depends on the root cause of the defect. Often the result details show a
sequence of events that led to the defect. You can implement the fix on any event in
the sequence. If the result details do not show the event history, you can trace
back using right-click options in the source code and see previous related events.
See also Interpret Bug Finder Results in Polyspace Desktop User Interface.
See examples of fixes below.
If you do not want to fix the issue, add comments to your result or code to avoid
another review. See:
Example - Incorrectly Placed Braces (C Only)
int arr[2][3]
= {{1, 2},
{3, 4},
{5, 6} //Noncompliant
};
In this example, the array arr
is initialized
as {1,2,0,3,4,0}
. Because the initializer contains {5,6}
,
you might expect the array to be initialized {1,2,3,4,5,6}
.
Correction — Place Braces CorrectlyOne possible correction is to place the braces correctly
so that all elements are explicitly
initialized.
int a1[2][3]
= {{1, 2, 3},
{4, 5, 6}
};
Example - First Element Not Explicitly Initializedint arr[5]
= {
[1] = 2,
[2] = 3,
[3] = 4,
[4] = 5
}; //Noncompliant
In this example, arr[0]
is not explicitly
initialized. It is possible that the programmer did not consider that
the array indexing starts from 0.
Correction — Explicitly Initialize All
ElementsOne possible correction is to initialize all
elements explicitly.
int arr[5]
= {
[0] = 1,
[1] = 2,
[2] = 3,
[3] = 4,
[4] = 5
};
Example - Element Initialized Twiceint arr[5]
= {
[0] = 1,
[1] = 2,
[2] = 3,
[2] = 4, //Noncompliant
[4] = 5
};
In this example, arr[2]
is initialized twice.
The first initialization is overridden. In this case, because arr[3]
was
not explicitly initialized, it is possible that the programmer intended
to initialize arr[3]
when arr[2]
was
initialized a second time.
Correction — Fix Redundant InitializationOne possible correction is to eliminate the redundant initialization.
int arr[5]
= {
[0] = 1,
[1] = 2,
[2] = 3,
[3] = 4,
[4] = 5
};
Example - Mix of Designated and Nondesignated Initializersint arr[]
= {
[0] = 1,
[3] = 3,
4,
[5] = 5,
6
}; //Noncompliant
In this example, because a mix of designated and nondesignated
initializers are used, it is difficult to determine the size of arr
by
inspection.
Correction — Use Only Designated InitializersOne possible correction is to use only designated
initializers for array initialization and to specify the size of the array
explicitly.
int arr[7]
= {
[0] = 1,
[3] = 3,
[4] = 4,
[5] = 5,
[6] = 6
};