# V2654. MISRA. Initializer list should not contain persistent side effects.

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

The rule is relevant only for C.

When declaring objects of structures, unions, or arrays, a programmer can provide initial values for their data members or elements using aggregate initialization (`{ .... }`).

In this case, the initializer list must not contain persistent side effects.

According to the MISRA C standard, a side effect is considered persistent if it can influence the program state at a specific point in execution. Examples of such effects include:

- modifying a file;
- modifying (writing to) an object;
- using memory barriers;
- modifying the floating-point environment;
- accessing a `volatile` object.

The initialization list does not specify the order of expression evaluation, so persistent side effects may lead to an unpredictable result.

Look at the example:

    int x = 0;

    int main()
    {
      int array[2] = { ++x, x=2 }; // array is expected to be { 1, 2 }
                                   // but could be { 1, 2 } or { 3, 2 }
      // x should equals 3
      // ....
    }

Since the order of expression evaluation in the initialization list is unspecified, it cannot be reliably asserted which values will be assigned to the array elements.

The fixed example:

    int x = 0;

    int main()
    {
      int array[2] = { x + 1, x + 2 }; // always will be { 1, 2 }
      x = 3;
      // ....
    }

This diagnostic is classified as:

MISRA-C-2023-13.1