﻿# V2632\. MISRA\. Object with temporary lifetime should not undergo array\-to\-pointer conversion\.

This diagnostic rule is based on the [MISRA](https://misra.org.uk/) \(Motor Industry Software Reliability Association\) software development guidelines\.

This diagnostic rule is relevant only for C\.

Arrays as temporary objects should not be converted to a pointer\. Temporary objects exist only for the duration of their complete value expression\. They are destroyed immediately after the value expression completion\.

An array can be a member of a structure or union and therefore form part of the result value of any value expression\. Because an array used in an expression is always decayed to a pointer, in C it is possible to form a pointer to an array that is a sub\-object of a temporary object\. Modification of temporary array elements, as well as accessing them after their lifetime, leads to [undefined behavior](https://pvs-studio.com/en/blog/terms/0066/)\.

Look at the code example:

```cpp
struct S
{
  int arr[10];
};

struct S getS(void);

void foo(int const *p);

void bar()
{
  p = getS().arr;         // <=
  foo(getS().arr);        // <=

  int j = getS().arr[3];
  getS().arr[3] = j;      // <=
}
```

The `S` structure contains the `arr` array of 10 elements as a data member\. The analyzer issues a warning when trying to access this array via the temporary object\.

To fix it, declare the object with a normal lifetime:

```cpp
struct S
{
  int arr[10];
};

struct S s;
struct S getS(void);

void foo(int const* p);

void global_object()
{
  int* p = s.arr;
  s.arr[0] = 1;
  p[1] = 1;

  foo(s.arr);
}

void local_object()
{
  struct S obj = getS();
  int* p = obj.arr;
  obj.arr[0] = 1;
  p[1] = 1;

  foo(obj.arr);
}
```