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

>
>
>
/Wp64 switch and template processing er…

/Wp64 switch and template processing error

Feb 05 2010

While maintaining the analyzer Viva64 (included into PVS-Studio) we often comment upon the switch /Wp64 of Microsoft Visual C++. If you are out of the swim, let me remind you that this switch appeared in Visual Studio 2003 and was intended for preparing migration of applications to 64-bit systems. In Visual Studio 2008, the switch /Wp64 is considered deprecated because it is high time we began to compile 64-bit applications instead of preparing for it. I.e. compilation in 64-bit mode reveals all the same code errors and bugs that the switch /Wp64 does when building a 32-bit application. And in the case of 64-bit code it is much more thorough and precise.

But besides that, /Wp64 switch has one more drawback that confuses the programmers who are not familiar with it. It concerns the issue of developing a code containing some templates. Here is an example.

In the vast ocean of the Internet you may find the following example in the comments to Visual C++ developers' blog:

vector<size_t> vs; // create the element vector size_t
vector<unsigned int> vi; // create the element vector unsigned int
size_t s; // there is a variable of size_t
unsigned int i; // there is a variable of unsigned int
vs[0] = s; // there must be no warning
vs[0] = i; // there must be no warning
vi[0] = s; // there must be warning (*0)
vi[0] = i; // there must be no warning
s = vs[0]; // there must be no warning
i = vs[0]; // there must be warning (*1)
s = vi[0]; // there must be no warning
i = vi[0]; // there must be no warning (*2)

Consider that the types size_t and unsigned int must coincide in 32-bit mode.

Now we compile this code in 32-bit mode in Visual C++ 2005 and get the following warnings. In the line marked with (*1) everything is alright:

warning C4267: '=' : conversion from 'size_t' to 'unsigned int', possible loss of data

But in the line marked with (*2) we also see the same warning:

warning C4267: '=' : conversion from 'size_t' to 'unsigned int', possible loss of data

Yet there should not be any warning here.

And in the line (*0) there is a missing warning.

But if you compile the code in 64-bit mode, you get the warning on the lines marked with (*0) and (*1) as it should be:

warning C4267: '=' : conversion from 'size_t' to 'unsigned int', possible loss of data

The author of the example, Stephan T. Lavavej, discusses the problems of implementing /Wp64 switch in templates. The point is that the compiler switch /Wp64 is implemented through the special key word __w64, added to the type description:

#ifdef _WIN64
  typedef __int64 MySSizet;
#else
  typedef int __w64 MySSizet; // Add __w64 keyword
#endif

But this key word does not introduce a new data type and that is why the template classes vs and vi in this code are identical:

typedef __w64 unsigned int   size_t;
vector<__w64 unsigned int> vs;
vector<unsigned int> vi;

And although vs and vi seem to have different types, the compiler considers them identical not without reason and generates false diagnostic warnings.

What to do? In Microsoft Connect there is an error, but, as they have written, they are not going to fix it. First, because they do not know how, and second, because it is relevant only to the switch /Wp64 that is announced deprecated and will be removed.

Although the analyzer Viva64 (included into PVS-Studio) we are developing is not very good at handling templates either, it still works correctly and generates the expected warnings for this code, but relying on other rules. In particular, if the warning V101 is enabled, it generates the warning when unsigned type is cast to size_t because it might hide an error. Thus, Viva64 analyzer will generate the following:

std::vector<size_t> vs;
std::vector<unsigned int> vi;
size_t s;
unsigned int i;
vs[0] = s;
vs[0] = i; //V101: Implicit assignment
           //type conversion to memsize type.
vi[0] = s; //V103: Implicit type conversion
           //from memsize to 32-bit type.
vi[0] = i;
s = vs[0];
i = vs[0]; //V103: Implicit type conversion
           //from memsize to 32-bit type.
s = vi[0]; //V101: Implicit assignment
           //type conversion to memsize type.
i = vi[0];

Still the analyzer may understand in some cases that some assignments are safe and reduce the number of false alarms. Here is an example:

std::vector<unsigned int> vi;
for (size_t i = 0; i < 10; i++)
  vi[i] = i;

The compiler generates the warning for this code:

warning C4267: '=' : conversion from 'size_t' to 'unsigned int', possible loss of data

But Viva64 analyzer takes into account that the value of the variable "i" lies within the range [0..10] and this code cannot cause an error. As a result, it does not generate any diagnostic warnings.

Popular related articles


Comments (0)

Next comments next comments
close comment form