V1009. Check the array initialization. Only the first element is initialized explicitly.


The analyzer has detected a possible error that has to do with initializing only the first element of an array during declaration. This means that the other elements will be implicitly initialized to zero or by the default constructor.

Consider the following example of incorrect code:

int arr[3] = {1};

The programmer probably expected that the 'arr' array would be filled with ones, but this assumption is wrong. The array will contain the elements 1, 0, 0.

Fixed code:

int arr[3] = {1, 1, 1};

Mistakes like this may result from confusing this declaration construct with the similarly looking "arr = {0}" construct, which initializes every element of the array to zero.

If such constructs are common in your project, you may want to disable this diagnostic.

It is also recommended that you keep your code clear.

For example, consider the following code defining color codes:

int White[3] = { 0xff, 0xff, 0xff };
int Black[3] = { 0x00 };
int Green[3] = { 0x00, 0xff };

Thanks to implicit initialization, all the colors are defined correctly, but it is better to rewrite this code in a more straightforward form:

int White[3] = { 0xff, 0xff, 0xff };
int Black[3] = { 0x00, 0x00, 0x00 };
int Green[3] = { 0x00, 0xff, 0x00 };

Bugs Found

Checked Projects
344
Collected Errors
12 970