So you think you know C++? Know enough to say out aloud in an interview, "I am expert in C++."? Even though I've got several good years of C++ behind me I wouldn't dare to, and the reason why I'm writing this is to show a few gotchas that can be quite subtle, in the form of a quiz. This post is dedicated to the cocky fellows who pride in their abilities in being knowledgeable low-level systems programmers who write fast code and sneer at higher-level languages with all their dynamic typing and garbage collectors. We all know the type.

Also, this post probably makes all the higher-level language fanatics put up a schadenfreude-laugh towards the C++ crowd. After all, most (not all) the problems shown here are artifacts of the C++ language solely. I don't want to steer the thoughts into any inter-language comparisons, as those so often lead nowhere. Instead the point is to show that humility really should be considered one of the good traits in a programmer.

The quiz is rather short, and a bit different from most of the quizzes I've read on the net. I've got 4 code snippets, from codebases both old and new. I've renamed some symbols and simplified the code listing from the original codebase to make it a lot shorter and simpler to follow so I've never actually run these versions of the code. There might be some syntax errors, which are not the point.

In order to follow typical 'development' situations, the quiz consists of two parts, which are presented below. The four cases, which you're tasked to analyze, can be opened from the links here:
  • Case 1: Setting the visual style of a list of UI buttons, using the UICanvas::SetStyleForVisibleButtons method.
  • Case 2: Logging diagnostic messages into output streams when doing 3D world object processing.
  • Case 3: Recursively computing values of the Pascal's triangle.
  • Case 4: Initializing a Direct3D10 device for rendering.

First Part


The first part consists of static code analysis. Just by reading the code cases above, you should be able to present what the problem is with the code. But there's a catch: Not all of the cases contain a bug (I'm not even giving you numbers!). For each individual case, the scoring from this part is as follows:

  • If you correctly identify the problem, award yourself 2 points.
  • If you identify a problem, and it turns out there was no bug at all, award yourself -1 point.
  • If you claim that there is no problem, and there is one, award yourself -1 point.
  • If you correctly claim that there is no problem, award yourself 1 point.

After finishing this part, mark your answers down, and proceed to the second part. After reading the second part, you are not allowed to go back and change your answers in the first part.

Second Part


As with real development, after writing the code you ship it in Release mode without testing it at all. Of course as expected, it didn't go too smooth. The angry end-user comes back to you with a (usually crappy) description of what happened in each case. Now, with this additional information you're asked to evaluate the four cases again. The scoring for each individual case in this part is as follows:

  • If you already answered right in the first part, do not count any score for those cases in this part.
  • If you weren't able to identify the problem in the first part, but with the help of the description now can, award yourself 1 point.
  • If you fail to identify the problem, you get no points, but none are subtracted from the total either.

The total score from the quiz is the sum of the scores from both parts. The descriptions are presented below. Note that you're not allowed to change the answers to the first part after reading this. 

  • Case 1: After several tries and double-checking that the stylesheets that are being fed to the buttons are appropriate and scalable, it still resulted that calling UICanvas::SetStyleForVisibleButtons fails to change the style. If the problem didn't lie in the stylesheets, where would it be then?
  • Case 2: The method World::RemoveAllInvalidGeometry is called whenever new geometry is imported into the world editor. It was noticed that sometimes the log would be empty even if there were invalid geometry present. After a while, the application would crash with the message "This program has performed an illegal operation and will be shut down.". Certainly there shouldn't be anything bad going on?
  • Case 3: The code was tested and found to work as expected. Congratulations if you were able to verify this.
  • Case 4: For some reason, running the code always crashes like in case 2 when instantiating the Graphics object in main(). Mysteriously enough, even commenting out the call to InitDisplay didn't seem to affect the matter. Where is the issue and what causes it?

In all fairness, and what makes most of the quizzes a bit lame, is that stepping through the actual code in a debugger would in many cases reveal the sources for the bugs quite easily. The maximum score for the quiz is 7 points, which you get by solving all the cases already in the first part. If you get a score of 6 or 7, you beat me (I estimate I would've gotten a 5 by solving only one of the cases in part one and being able to spot the bugless one, but I'm already biased). If you get less than 3, that is, weren't able to find any of the bugs even after reading the reports, then I'd go and order out the book of the standard. In any case, don't forget humility.

Oh, and where are the answers? Wasn't it too obvious and you scored a 7 and don't even need the answers? Or not? Well, perhaps I'll post them up at a later date. :) 

Last Updated (Wednesday, 14 January 2009 23:40)