Unicorn with delicious cookie
Nous utilisons des cookies pour améliorer votre expérience de navigation. En savoir plus
Accepter
to the top
>
>
>
What is sizeof(&X) expression equal to,…

What is sizeof(&X) expression equal to, X being defined as "char *X[n];"?

15 Mar 2011
Author:

Consider the following sample.

char *(X[64]);
cout << sizeof(&X) << endl;

The question is: what value will be printed? The right answer is "the pointer's size". In particular, it may be number 4 in a Win32 program or 8 in a Win64 program.

The answer above seems obvious, yet there are two subtleties which often cause confusion. Let's examine another sample:

char *(X[64]);
memset(&X, 0, sizeof(&X));

This code is incorrect: we empty only a part of the X array. There are two reasons for such errors.

Reason one

The VS 2005 compiler has an error that causes "sizeof(&X)" to return the array's size. As a result, this code, being built in VS 2005, will correctly empty the whole array. Accordingly, some programmers get misled and consider this code correct. The compiler's error disappears after installing SP1.

By the way, here you are an interesting test on the subject I encountered in the code of Google C++ Mocking Framework:

class TestForSP1 {
private: // GCC complains if x_ is used by sizeof before defining it.
  static char x_[100];
  // VS 2005 RTM incorrectly reports sizeof(&x) as 100, and that value
  // is used to trigger 'invalid negative array size' error. If you
  // see this error, upgrade to VS 2005 SP1 since Google Mock will not
  // compile in VS 2005 RTM.
  static char 
  Google_Mock_requires_Visual_Studio_2005_SP1_or_later_to_compile_[
      sizeof(&x_) != 100 ? 1 : -1];
};

Reason two

The first function's argument is "&X". Actually taking of the address is not necessary here. We are handling the array all the same. That is, the following two records have the same correct result:

memset(&X, 0, sizeof(X));
memset(X, 0, sizeof(X));

This also confuses programmers and they think that sizeof(&X) and sizeof(X) will have the same result too. But it is a mistake.

Popular related articles

S'abonner

Comments (0)

close comment form
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