Main Content

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.

IssueRiskPossible 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:

Examples

expand all


int arr[2][3]
= {{1, 2},
    {3, 4},
    {5, 6}
};

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 Correctly

One 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}
};
int arr[5]
= {
    [1] = 2,      
    [2] = 3,
    [3] = 4,
    [4] = 5
};              

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 Elements

One possible correction is to initialize all elements explicitly.

int arr[5]
= {
    [0] = 1,
    [1] = 2,      
    [2] = 3,
    [3] = 4,
    [4] = 5
};              
int arr[5]
= {
    [0] = 1,
    [1] = 2,      
    [2] = 3,
    [2] = 4,
    [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 Initialization

One possible correction is to eliminate the redundant initialization.

int arr[5]
= {
    [0] = 1,
    [1] = 2,
    [2] = 3,
    [3] = 4,
    [4] = 5
};
int arr[]
= {
    [0] = 1,
    [3] = 3,
    4,
    [5] = 5,
    6
    };              

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 Initializers

One possible correction is to use only designated initializers for array initialization.

int arr[]
= {
    [0] = 1,
    [3] = 3,
    [4] = 4,
    [5] = 5,
    [6] = 6
};              

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