Our website uses cookies to enhance your browsing experience.
Accept
to the top
>
>
>
V2626. MISRA. The 'sizeof' operator sho…
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

V2626. MISRA. The 'sizeof' operator should not have an operand which is a function parameter declared as 'array of type'.

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.

The sizeof operator returns the size of a pointer instead of the size of an array when the array is passed to the function by copy.

Look at the following code snippet:

char A[100];

void Foo(char B[100])
{
}

In this code fragment, the A object is an array, and the sizeof(A) expression returns the value of 100.

The B object is a regular pointer, despite its declaration. The 100 value in brackets is just a hint to a programmer that the passed array must contain one hundred elements. Thus, the sizeof(B) expression will be equal to the size of the pointer, which is implementation-defined. For example, for 32-bit systems, its size is 4 bytes and for 64-bit systems—8 bytes.

The warning is issued when the size of a pointer—passed as an argument in the array_type name[N] format—is evaluated. Most likely, such code contains an error. Look at the example:

void Foo(float array[3])
{
  const size_t n = sizeof(array) / sizeof(array[0]);
  for (size_t i = 0; i < n; ++i)
    array[i] = 0.0f;
}

The sizeof(array) / sizeof(array[0]) expression evaluates the array size incorrectly. As a result, it will not be completely filled with the 1.0f value.

To prevent such errors, pass the number of array elements explicitly.

The fixed code:

void Foo(float *array, size_t count)
{
  for (size_t i = 0; i < count; ++i)
    array[i] = 1.0f;
}
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