Improper array initialization
Incorrect array initialization when using initializers
Description
This defect 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. |
Fix
The fix depends on the root cause of the defect. See fixes in the table above and code examples with fixes below.
If you do not want to fix the issue, add comments to your result or code to avoid another review. See:
Address Results in Polyspace User Interface Through Bug Fixes or Justifications if you review results in the Polyspace user interface.
Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access) if you review results in a web browser.
Annotate Code and Hide Known or Acceptable Results if you review results in an IDE.
Examples
Result Information
Group: Programming |
Language: C | C++ |
Default: On for handwritten code, off for generated code |
Command-Line Syntax: IMPROPER_ARRAY_INIT |
Impact: Medium |
Version History
Introduced in R2015b
See Also
Topics
- Interpret Bug Finder Results in Polyspace Desktop User Interface
- Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access)
- Address Results in Polyspace User Interface Through Bug Fixes or Justifications
- Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access)