Stupid Interview Questions Everyone Still Asks: C/C++ Volatile and Mutable

Stupid Interview Questions is a recurring series about frequently asked interview questions that are, regardless of the self satisfied smirk that accompanies them, just not a good idea. This series looks to help engineers know more than just the answer, but also why the question was asked in the first place.
Click here to see them all
Click here to request that your question be covered

EDIT – 02/13/2011: If you’re getting ready to flame me as part of an elaborate love letter to the keyword volatile read this first. This author does a better job of explaining the point I’m trying to make. I muck it up a bit by trying to be “colorful”. You might also read this from the Linux documentation. After that you might also read this or this or maybe this or perhaps this.

Anyone that has interviewed for an engineering position in the US should be familiar with this scenario: it’s round 3 of a multi-week screening process and you’re sitting on the wrong side of an interview table. It’s 8 am, and you’re a few lines of pleasantries away from 6 straight hours of rapid fire questions. You’ll reinvent the history of math using smelly whiteboard markers. You’ll wrestle with obtuse hypothetical scenarios involving monkeys, doors, and any number of buckets and quarts of milk. Sure there will be a lunch break, but expect to keep scrambling for answers, a kung pao chicken peanut inexplicably lodged halfway up your nose.

A lot of these interview questions everybody asks, and many of these serve a useful purpose. However, a great many more are the geek world equivalent of a frat house hazing, a rite of passage. And quite frankly, that’s pretty stupid.

What follows is part one in a recurring series on interview questions that basically everyone asks in some form. Some are not so bad. But most are about as useful as taking a vigorous paddling from the Brosephs at Phi Beta Sigma.

Volatile and Mutable

The Question:

In C/C++ what do the keywords volatile and mutable mean? When would you use them?

The Real Answer:

They are trivia-level obscure variable keywords used to tell the compiler that you’re being a jackass and breaking all the rules. As for the follow up question, if you’re doing your job correctly you should probably use them never, ever, ever.

OK obviously they’re in the language for a reason: volatile is used to indicate that the variable should never be put into a register under the hood, while mutable means a class member can violate the rules of const, i.e. be modified from a const method. Does your knowledge of the legit uses of these keywords accurately measure your worth as an engineer? I’ll bet not.


There are times when volatile is appropriate to use. For example when you know the exact memory address of a hardware register and you map it to a local or global variable. In another lifetime I did some homebrew stuff on the Game Boy Advance without the luxury of an SDK. Certainly there were valid uses of volatile there since we were manipulating hardware memory directly (hacking the crap out of everything), but our youthful indiscretions never left a few sparse Internet forums. However, for the sake of argument if you’re writing an official hardware driver there may be times when volatile is your own personal necessary evil.

Fine then. Now, assuming you’re still sitting at that interview table, 4 or more conspicuously silent engineers across from you, look at your resume and tell me the position you’re applying for. Is it some vanilla software engineering position that puts you a country mile from the guts of the hardware? I thought so.

Truthfully volatile is more often seen in bad multithreading code, when programmers can’t be bothered to use built-in control structures like a mutex. Don’t get me wrong, it can be an appropriate optimization tool in limited known, high traffic situations where additional safety isn’t required. But those are spot optimizations that should be done after the code design is finished and tested, and not part of any old engineer’s starting process.

Widespread use of volatile is often a red flag rather than a programmer’s friend, indicating a wide variety of hacks.


Now mutable has a less obscure, actual textbook example, and you’d damn well better followup with it when answering this question in an interview. In this case use mutable on static class members used for ref counting if want to increment/decrement them from within a const method. Technically this is OK since you’re maintaining the spirit of const even though you’re actually breaking the rules. God only knows what you’re counting and why (I can only assume the number of times that certain methods are called for some reason), but fine. Whatever.

Forget for a moment that in 14 years of C++ programming I have never once used the mutable keyword at all, let alone implemented this particular example. Instead, just consider that you’re taking a static member variable, for all purposes a global variable in sheep’s clothing, and are using a keyword to forever remove one of it’s only interesting features. Does that sound like progress towards stronger design ideals?

Now tell me if you think knowledge of mutable is an indicator of a careful and skilled engineer, or that the presence of mutable in your codebase in an indicator of health or elegance. I’m guessing the answer is only the fattest of chances.

So Why Bother Asking It?

To put it succinctly: because it’s obscure. You could argue that the more trivia you know about a language the greater the implied skill and familiarity, but really you’re just testing whether they’ve read the C++ FAQ, or have been in at least one other interview. Ironically, someone that has extensive, hands on experience with either of these keywords (and thus the only legit candidate type to answer the question), is probably not the person you want to hire. At least not unless they make an ugly face first and dole out those examples as though describing a murder scene.

Should every interview room in the US ask this question?


Do they ask it anyway?


So learn it. Impress your friends and win women with your knowledge of near-useless information. But if you’re in the position to give your own interviews don’t pass on the disease. Keep mutable, violatile, and other trivia questions where they belong: in your head next to your vast collection of “Family Guy” quotes.