A common trap for C++ interviews

A common trap for C++ interviews

Are you good enough at C++ to answer a question at an interview that most people can't answer? As we all know, the technology in IT as well as the entire IT-world keeps changing very fast. The same goes for the programming language C++, which has been developed a lot since 2011.

The C++ 17-standard is currently used a lot in many different production environments, but also earlier versions are still common. Therefore it's difficult for interviewers to check whether or not an applicant knows all the pros and cons of the different standards. But let's come to the point now!

Move semantic is recognized as one of the most important of the 11 introduced functionalities of C++. Here is an easy, yet very interesting question that is often asked at an interview:

“What is move semantic? What does the method std::movedo? “

And that is the moment when the party begins. The overwhelming majority of people would start their answer with something like:

“It moves underlying storage or reference-points from one object to another…”

That answer can be - depending on how precise you're asking - checked using an example:

std::vector< int > vec{1,2,3,4,5};

auto mvec = std::move(vec);

std::cout << vec.size() << std::endl;

std::cout << mvec.size() << std::endl;

Looks like pretty easy code, a vector with five integers, lastly two lines for output and then another with std::moveto finish. But now come the treacherous questions:

“Will the code compile?"

Most of the time, this answer will come:

“Sure it will!”

Aye aye captain obvious, it actually compiles. But then it continues:

“Does the code work as intended?"

That is the moment when the applicant raises they eyebrow and starts doubting themselves. They might think something like this: “Two sentences ago I said that underlying storage or reference-points are moved, so the vec variable will become invalid.” But here comes the next question:

“But the stack-memory won't be invalid?”

After so many questions, many people just throw in the towel, because they have no possible solution at hand. They thought that they knew roughly how thestd::moveoperator works, but they appearently have no idea what is happening at all.

Don't worry, here's the answer to the question, so you don't fall into this trap at you next interview:

std::moveis a remove-reference-cast-to-rvalue operato. Basically, it ensures that the move constructor, as long as that is avaibale on the left side, is called upon with the argument from the right side.

For our example this means: The move constructor of the variable mvecis called upon with the variablevec as the argument. Move “steals” the internal memory objects, because the vector's inside is compiled of three pointers:

  • _M_start, ist nullptr
  • _M_finish, ist nullptr
  • _M_end_of_storage, ist nullptr

But wait, what does “steal” mean now? The move constructor does the following:

The data is swapped(not assigned, not comped, but swapped). This is the fastest and most efficient way. Therefore, the old vector vecwill be empty, and the new vector mvecwill contain the elements previously held by vec. Because of that, the deallocation of both vectors works as if the properly assigned pointer was only available on one position.

Understood? In reality, it's a rather simple answer, no need to let treacherous questions unsettle you :)

Technologies in this article

This might also interest you