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

The diagnostic rule is based on the [MISRA](https://misra.org.uk/) \(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](https://en.cppreference.com/w/c/language/initialization.html) must not contain persistent [side effects](https://en.cppreference.com/w/c/language/eval_order.html)\.

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](https://en.cppreference.com/w/c/header/stdatomic.html);
* modifying the [floating\-point environment](https://en.cppreference.com/w/cpp/numeric/fenv.html);
* 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:

```cpp
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:

```cpp
int x = 0;

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