Our website uses cookies to enhance your browsing experience.
Accept
to the top
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

Webinar: Evaluation - 05.12

>
>
>
What is sizeof(&X) expression equal to,…

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

Mar 15 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.



Comments (0)

Next comments next comments
close comment form