Our website uses cookies to enhance your browsing experience.
Accept
to the top
>
>
>
V2632. MISRA. Object with temporary lif…
menu mobile close menu
Analyzer diagnostics
General Analysis (C++)
General Analysis (C#)
General Analysis (Java)
Micro-Optimizations (C++)
Diagnosis of 64-bit errors (Viva64, C++)
Customer specific requests (C++)
MISRA errors
AUTOSAR errors
OWASP errors (C++)
OWASP errors (C#)
OWASP errors (Java)
Problems related to code analyzer
Additional information
toggle menu Contents

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

Feb 07 2025

This diagnostic rule is based on the MISRA (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.

Look at the code example:

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:

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);
}
close form

Fill out the form in 2 simple steps below:

Your contact information:

Step 1
Congratulations! This is your promo code!

Desired license type:

Step 2
Team license
Enterprise license
** By clicking this button you agree to our Privacy Policy statement
close form
Request our prices
New License
License Renewal
--Select currency--
USD
EUR
* By clicking this button you agree to our Privacy Policy statement

close form
Free PVS‑Studio license for Microsoft MVP specialists
* By clicking this button you agree to our Privacy Policy statement

close form
To get the licence for your open-source project, please fill out this form
* By clicking this button you agree to our Privacy Policy statement

close form
I am interested to try it on the platforms:
* By clicking this button you agree to our Privacy Policy statement

close form
check circle
Message submitted.

Your message has been sent. We will email you at


If you do not see the email in your inbox, please check if it is filtered to one of the following folders:

  • Promotion
  • Updates
  • Spam