To get a trial key
fill out the form below
Team License (a basic version)
Enterprise License (extended version)
* By clicking this button you agree to our Privacy Policy statement

Request our prices
New License
License Renewal
--Select currency--
* By clicking this button you agree to our Privacy Policy statement

Free PVS-Studio license for Microsoft MVP specialists
* By clicking this button you agree to our Privacy Policy statement

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

I am interested to try it on the platforms:
* By clicking this button you agree to our Privacy Policy statement

Message submitted.

Your message has been sent. We will email you at

If you haven't received our response, please do the following:
check your Spam/Junk folder and click the "Not Spam" button for our message.
This way, you won't miss messages from our team in the future.

/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;
  typedef int __w64 MySSizet; // Add __w64 keyword

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
Technologies used in the PVS-Studio code analyzer for finding bugs and potential vulnerabilities

Date: Nov 21 2018

Author: Andrey Karpov

A brief description of technologies used in the PVS-Studio tool, which let us effectively detect a large number of error patterns and potential vulnerabilities. The article describes the implementati…
The way static analyzers fight against false positives, and why they do it

Date: Mar 20 2017

Author: Andrey Karpov

In my previous article I wrote that I don't like the approach of evaluating the efficiency of static analyzers with the help of synthetic tests. In that article, I give the example of a code fragment…
Appreciate Static Code Analysis!

Date: Oct 16 2017

Author: Andrey Karpov

I am really astonished by the capabilities of static code analysis even though I am one of the developers of PVS-Studio analyzer myself. The tool surprised me the other day as it turned out to be sma…
How PVS-Studio Proved to Be More Attentive Than Three and a Half Programmers

Date: Oct 22 2018

Author: Andrey Karpov

Just like other static analyzers, PVS-Studio often produces false positives. What you are about to read is a short story where I'll tell you how PVS-Studio proved, just one more time, to be more atte…
The Ultimate Question of Programming, Refactoring, and Everything

Date: Apr 14 2016

Author: Andrey Karpov

Yes, you've guessed correctly - the answer is "42". In this article you will find 42 recommendations about coding in C++ that can help a programmer avoid a lot of errors, save time and effort. The au…
Static analysis as part of the development process in Unreal Engine

Date: Jun 27 2017

Author: Andrey Karpov

Unreal Engine continues to develop as new code is added and previously written code is changed. What is the inevitable consequence of ongoing development in a project? The emergence of new bugs in th…
The Last Line Effect

Date: May 31 2014

Author: Andrey Karpov

I have studied many errors caused by the use of the Copy-Paste method, and can assure you that programmers most often tend to make mistakes in the last fragment of a homogeneous code block. I have ne…
Characteristics of PVS-Studio Analyzer by the Example of EFL Core Libraries, 10-15% of False Positives

Date: Jul 31 2017

Author: Andrey Karpov

After I wrote quite a big article about the analysis of the Tizen OS code, I received a large number of questions concerning the percentage of false positives and the density of errors (how many erro…
PVS-Studio ROI

Date: Jan 30 2019

Author: Andrey Karpov

Occasionally, we're asked a question, what monetary value the company will receive from using PVS-Studio. We decided to draw up a response in the form of an article and provide tables, which will sho…
Free PVS-Studio for those who develops open source projects

Date: Dec 22 2018

Author: Andrey Karpov

On the New 2019 year's eve, a PVS-Studio team decided to make a nice gift for all contributors of open-source projects hosted on GitHub, GitLab or Bitbucket. They are given free usage of PVS-Studio s…

Comments (0)

Next comments
This website uses cookies and other technology to provide you a more personalized experience. By continuing the view of our web-pages you accept the terms of using these files. If you don't want your personal data to be processed, please, leave this site.
Learn More →