Unicorn with delicious cookie
Nous utilisons des cookies pour améliorer votre expérience de navigation. En savoir plus
Accepter
to the top
>
>
>
V2612. MISRA. Array element should...
menu mobile close menu
Additional information
toggle menu Contents

V2612. MISRA. Array element should not be initialized more than once.

09 Sep 2021

This diagnostic rule is based on the MISRA (Motor Industry Software Reliability Association) guidelines for software development.

This rule applies only to C. The C language has special syntax to initialize expressions - a designated initializer. It allows you to initialize array or structure elements in a custom order.

For example, you can initialize an array's specific elements:

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

This syntax also works for structures:

struct point
{
  int x;
  int y;
};

struct point pt1 = {
  .x = 1,
  .y = 1,
};

However, when using this syntax, a developer can make a mistake and initialize the same element twice:

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

struct point pt1 = {
  .x = 1,
  .x = 1,
};

MISRA prohibits this syntax construction, because the language standard does not define whether the code above causes any side effects. Most likely, such mistakes are typos.

This diagnostic is classified as:

  • MISRA-C-9.4