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

** This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
Request our prices
New License
License Renewal
--Select currency--
USD
EUR
GBP
RUB
* By clicking this button you agree to our Privacy Policy statement

** This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
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

April 3, 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.

0722_job_interview/image1.png

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 (hh.ru). 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:

0722_job_interview/image2.png

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 { .... };
std::unique_ptr<int>::unique_ptr
std::unique_ptr<double>::unique_ptr

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:

0722_job_interview/image4.png

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
The Last Line Effect

Date: 05.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: 07.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…
Static analysis as part of the development process in Unreal Engine

Date: 06.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…
How PVS-Studio Proved to Be More Attentive Than Three and a Half Programmers

Date: 10.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…
Appreciate Static Code Analysis!

Date: 10.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…
The Evil within the Comparison Functions

Date: 05.19.2017

Author: Andrey Karpov

Perhaps, readers remember my article titled "Last line effect". It describes a pattern I've once noticed: in most cases programmers make an error in the last line of similar text blocks. Now I want t…
Technologies used in the PVS-Studio code analyzer for finding bugs and potential vulnerabilities

Date: 11.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…
PVS-Studio ROI

Date: 01.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 way static analyzers fight against false positives, and why they do it

Date: 03.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…
Free PVS-Studio for those who develops open source projects

Date: 12.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 site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
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 →
Accept