To get a trial key
fill out the form below
Team License (a basic version)
Enterprise License (an 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.

How deep the rabbit hole goes, or C++ j…

How deep the rabbit hole goes, or C++ job interviews at PVS-Studio

Apr 03 2020

Here's an interesting story about how one of the questions we ask at job interviews turned out to reach even deeper than intended by its author. You've always got to watch your step with C++ and C++ compilers – there's never a risk of getting bored.


Just like any other software developer company, we have our own lists of questions to ask applicants for C++, C#, and Java developer positions. Many of these questions are tricky, and quite a bit so. Well, we can't guarantee that with questions on C# and Java as they were devised by other authors. But many of the questions on C++ authored by Andrey Karpov were definitely aimed at probing the depth of the applicant's knowledge of language specifics.

There's a simple answer to any of those questions. But it can be elaborated to one degree or another. By the depth of the applicant's answer we can tell how well they know the language peculiarities. This is extremely important to us because developing a code analyzer requires a profound understanding of all the technicalities and "quirks" of the language.

Today's little story is about one of the first questions we ask at interviews and how it turned out to run even deeper than we intended. Now, we show this code to the applicant:

void F1()
  int i = 1;
  printf("%d, %d\n", i++, i++);

and ask him or her, "What do you think this code will print?"

That's a good question. The answer says a lot about the applicant's expertise. Those who are too incompetent to answer at all don't count – we have them filtered off through pre-interview testing on HeadHunter ( Oh, wait... We actually had a couple odd applicants who replied something like this:

This code will first print a percent sign, then a d, then percent sign and d again, then backslash and n, and then two ones.

Needless to say, that was the end of the interview.

Now, back to our normal interviews :). Applicants will often say:

This code will print 1 and 2.

This is a trainee's answer. Sure, the program could print these values, but what we want to hear is something along these lines:

You can't say for sure what exactly this code will print. This is unspecified (or undefined) behavior. The order of evaluating the arguments is not defined. All the arguments are to be evaluated before the called function's body executes, but the exact order of their evaluation is compiler-specific. So this code might well output "1, 2" or "2, 1". In any case, it's strongly recommended not to write code like that if you build it with at least two compilers because you risk "shooting yourself in the foot". Many compilers would actually issue a warning on that.

Indeed, Clang may output "1, 2".

And GCC may output "2, 1".

A long time back, we tried the MSVC compiler, and it would print "2, 1" too. There was nothing to worry about.

But some time ago, we needed to compile that sample code using the modern version of Visual C++ for an entirely different reason. We built it in the Release configuration with the /O2 optimization level. We weren't seeking trouble but it found us anyway :). What do you think we got? Ha! We got "1, 1".

We could only wonder at what we saw. The problem turned out to be much more intricate than we could ever think.

Since the C++ standard does not prescribe an exact order of argument evaluation, the compiler interprets this type of unspecified behavior in a very peculiar way. Let's take a look at the assembly generated by the MSVC 19.25 compiler (Microsoft Visual Studio Community 2019, Version 16.5.1), with the standard version flag '/std:c++14' enabled:


Technically, the optimizer turned the code above into the following:

void F1()
  int i = 1;
  int tmp = i;
  i += 2;
  printf("%d, %d\n", tmp, tmp);

From the compiler's point of view, this optimization doesn't alter the program's observable behavior. When I look at this, I begin to understand the point of adding the "magic" function make_shared, along with the smart pointers, in C++11 (and make_unique in C++14). Here's another seemingly harmless snippet that gets screwed up too:

void foo(std::unique_ptr<int>, std::unique_ptr<double>);

int main()
  foo(std::unique_ptr<int> { new int { 0 } },
      std::unique_ptr<double> { new double { 0.0 } });

A crafty compiler may enforce the following evaluation order (which is what MSVC did):

new int { .... };
new double { .... };

If the second call of the new operator throws an exception, we'll get a memory leak.

But let's get back to the original example. Even though all was fine from the compiler's point of view, the string "1, 1" still didn't seem to be the right output from the programmer's perspective. So we compiled the source code with MSVC, with the '/std:c++17' flag on. This worked as expected, and the program printed "2, 1". Here's the assembly:


Now the compiler doesn't try to be original and passes the values 2 and 1 for the arguments, as expected. But why did changing the standard flag affect the code so drastically? It turns out the C++17 standard has the following addition:

The postfix-expression is sequenced before each expression in the expression-list and any default argument. The initialization of a parameter, including every associated value computation and side effect, is indeterminately sequenced with respect to that of any other parameter.

The compiler still has the right to evaluate arguments in an arbitrary order, but starting with the C++17 standard, it can move on to evaluate the next argument and its side effects no sooner than it has finished evaluating the previous argument and its side effects.

By the way, the '/std:c++17' flag helps fix that other example with smart pointers too, without having to use std::make_unique.

That's how we discovered another level of depth to the question. It's theory vs practice in the form of a specific compiler or differing interpretations of the standard :). The C++ world has always been much more complex and surprising than you might think.

If you have a more accurate explanation of that behavior, please let us know in the comments. We can't rest until we've figured it all out – if only to know the right answer before asking another applicant! :)

That's a story we all can learn from. I hope you enjoyed reading this, and please don't hesitate to share your opinion. We recommend using the most recent language standard if you don't want to be amazed at the tricks the modern optimizing compilers can do. Or better still – don't write code like that at all :).

P.S. You may think we'll have to drop this question from our list now that we "disclosed" it. But we don't see why we should do that. If the applicant took the trouble to read our articles before going to the interview and used what he or she had learned from this particular one, it would earn them a score for doing a good job and increase their chances to get hired :).

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 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…
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…
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…
PVS-Studio for Java

Date: Jan 17 2019

Author: Andrey Karpov

In the seventh version of the PVS-Studio static analyzer, we added support of the Java language. It's time for a brief story of how we've started making support of the Java language, how far we've co…
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…
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…
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…
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…
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…

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 →