Unicorn with delicious cookie
Nous utilisons des cookies pour améliorer votre expérience de navigation. En savoir plus
Accepter
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'.

07 Fév 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;
}

This diagnostic is classified as:

  • MISRA-C-12.5
close form

Remplissez le formulaire ci‑dessous en 2 étapes simples :

Vos coordonnées :

Étape 1
Félicitations ! Voici votre code promo !

Type de licence souhaité :

Étape 2
Team license
Enterprise licence
** En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité
close form
Demandez des tarifs
Nouvelle licence
Renouvellement de licence
--Sélectionnez la devise--
USD
EUR
* En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité

close form
La licence PVS‑Studio gratuit pour les spécialistes Microsoft MVP
close form
Pour obtenir la licence de votre projet open source, s’il vous plait rempliez ce formulaire
* En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité

close form
I want to join the test
* En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité

close form
check circle
Votre message a été envoyé.

Nous vous répondrons à


Si l'e-mail n'apparaît pas dans votre boîte de réception, recherchez-le dans l'un des dossiers suivants:

  • Promotion
  • Notifications
  • Spam