News for hackers. Stuff that matters.
Re:The Best C++ Interview Question – Ever by tptacek on Monday December 07, 2009 @04:59PM (#982282)
This is a completely broken C++ interview question. It asks the candidate to reason through a worst-practice, which exposes you to their knowledge of trivia and tells you nothing about whether they can actually get stuff done in C++.

My old Sonicity C++ code, which was built on ACE, is littered with "delete this" calls for Reactor callbacks. Obviously, "delete this" is OK if you're on the heap and you can guarantee that the code isn't going to touch the object again. But what I remember most about that ACE Reactor code was how fragile the event handling was; "delete this" is a recipe for horrible memory corruption flaws.

This is what you get when your C++ interviewers decide that interviews are more about proving how smart they are than about qualifying candidates.

Re:The Best C++ Interview Question – Ever by coffeemug on Monday December 07, 2009 @05:36PM (#982381)
I disagree, I think this actually a pretty good question, if asked properly. If you expect the right answer right away, then yes, it's a useless piece of trivia. But if you ask people to think it through, you might uncover a surprising lack of understanding of what's going on under the hood (stack vs. heap, etc.) If your goal is to have people think it through and explain their thinking along the way, it's not a bad question at all (assuming your goal is to hire someone who knows C++ well - whether it's a good goal is a whole different question).
Re:The Best C++ Interview Question – Ever by tptacek on Monday December 07, 2009 @05:38PM (#982387)
Even with heap-allocated objects there are lots of cases where you can't safely "delete this", which is what makes it a worst-practice. If you just want to know whether the candidate knows what the heap is, ask them to explain why alloca() is evil.
Re:The Best C++ Interview Question – Ever by yan on Monday December 07, 2009 @05:45PM (#982404)
The more I stare at that question, the more I can't figure out if you mean the obvious or the subtle pitfalls of alloca(). Off the top of my head, some of the potential "evils" I can come up with is: automatic deallocation after the function (and not the scope iirc) RETs, difficulty in passing alloca-allocated memory to other functions, being totally non-thread safe, looks foreign to other devs, classic stack-allocated buffer attacks are easier on certain systems, more pernicious effects from off-by-one errors. Did you have anything else in mind?
Re:The Best C++ Interview Question – Ever by tptacek on Monday December 07, 2009 @05:49PM (#982415)
Ok, alloca() is a really bad question too.
Re:The Best C++ Interview Question – Ever by pavlov on Monday December 07, 2009 @06:18PM (#982495)
Why is alloca() non-thread-safe? Each thread has its own stack.
Re:The Best C++ Interview Question – Ever by yan on Monday December 07, 2009 @06:28PM (#982512)
The way I thought about it, concurrent access to objects allocated via alloca() would probably not be explicitly protected by a lock. Threads can also be killed and spawned while other threads are accessing its stack contents. Anyway, I can think of more use cases where it's a bad idea to allocate objects on thread's stacks that you're planning to share.
Re:The Best C++ Interview Question – Ever by pavlov on Monday December 07, 2009 @07:07PM (#982599)
Yeah, sharing objects on the thread-local stack is definitely a bad idea.

But as long as the alloca'd memory is only accessed from within the same thread, it is -- AFAIK -- safe to call alloca() simultaneously from multiple threads, the same as malloc(). I think that qualifies as thread-safety by the usual definition: i.e. the function doesn't access statics or globals.

(Of course this will vary depending on the CRT. I don't think any UNIXes nowadays ship without threading support in the CRT, but Microsoft Visual C++ used to have separate thread-safe and non-thread-safe C libraries.)

Re:The Best C++ Interview Question – Ever by scott_s on Monday December 07, 2009 @05:19PM (#982341)
If someone can explain what happens, then one of two things is probably true: they've purposefully memorized an edge case for the sake of trivia, or their knowledge of language semantics and runtime behavior is deep enough to figure out what happens in an unusual circumstance.

I agree that doesn't tell you if the candidate is good at solving problems using C++, but there is potential to discover how deep their understanding is.

Re:The Best C++ Interview Question – Ever by tptacek on Monday December 07, 2009 @05:28PM (#982351)
It also selects for developers who deliberately code up to the limit of what the runtime will allow, regardless of the obvious fact that having code like this is like sprinkling landmines across your product.
Re:The Best C++ Interview Question – Ever by scott_s on Monday December 07, 2009 @05:32PM (#982366)
You're conflating actually doing it with being able to reason about what would happen based on prior understanding.
Re:The Best C++ Interview Question – Ever by flaub on Monday December 07, 2009 @05:54PM (#982428)
When you're looking for an expert in a field, do want to find someone that claims to know what they're doing because they read a book somewhere? Or do you want to find a seasoned pro that has actually tried and possibly failed using different techniques? Their failures may give them more insight and experience than their successes. Especially if they can tell you what they did to improve themselves afterwards.
Re:The Best C++ Interview Question – Ever by Retric on Monday December 07, 2009 @06:52PM (#982566)
Becoming an expert has far more to do with knowing how to get things done in a known time frame than knowing every little piece of triva. One you code in the same language with 3 or 4 compilers you start to look at edge cases as a dangerous no mans land to be avoided if at all possible.

PS: I worked with code from the 1984 Macintosh days that was still in use in 2005. You could see where people had updated from Motorola 68020 to PPC and if it had not been stripped out some poor coder might have updated the remaining ASM to x86.

Re:The Best C++ Interview Question – Ever by pivo on Monday December 07, 2009 @07:08PM (#982601)
It's been years since I worked with C++, but my recollection is that C++ programming involved a lot of memorization of edge cases just as a guard against running into them.
Re:The Best C++ Interview Question – Ever by philwelch on Monday December 07, 2009 @06:06PM (#982454)
It's probably a good interview question for a language lawyer.
Re:The Best C++ Interview Question – Ever by kscaldef on Monday December 07, 2009 @07:05PM (#982591)
The question is not whether or not the language spec permits you to do this. The question is about what happens if you do.
Re:The Best C++ Interview Question – Ever by philwelch on Monday December 07, 2009 @08:53PM (#982785)
...it's probably a good interview question for a C++ compiler programmer?

I just figured a good language lawyer, like a good real lawyer, would be able to both interpret the law (spec), but also tell you what would happen in reality if you violated it a certain way.

Re:The Best C++ Interview Question – Ever by yan on Monday December 07, 2009 @05:18PM (#982338)
This is a bad interview question for another reason -- I don't consider myself to be a good C++ developer and I answered it correctly. This is only because I reversed enough C++ code to understand how memory allocation, vtables and compilation works and have a decent grasp on implementation of object oriented languages. Would I make a great C++ developer though? Probably not in my definition of a "great developer."
Re:The Best C++ Interview Question – Ever by flaub on Monday December 07, 2009 @05:43PM (#982398)
For me, I've always thought that the 'greatest developer' was really the one that was the greatest at debugging. Any developer can write code, but my experience has shown that only a subset can properly diagnose issues to find their root cause. Those on the team that can do that will save you huge amounts of time and money because things will go wrong (especially in C++).

If you add up all the time it takes to write a particular system, I think you'll find debugging and fixing bugs to be one of the largest chunks of time, probably beating out the time it actually took you to write and possibly design the system. Obviously a design more suited to maintainability will decrease this somewhat.

Given this, I'd say this is an excellent question. Especially if the interviewee can tell you why it's a bad idea, or what the gotchas are with using 'delete this;'. It might not find developers with other positive characteristics like what makes for simple and elegant designs. But if you have a mix of questions to cover the many aspects of development in the interview, you should be good to go.

Re:The Best C++ Interview Question – Ever by tptacek on Monday December 07, 2009 @05:49PM (#982419)
Standard C/C++ interview question for me for the past few years: "how would you go about debugging a program that segfaulted inside of malloc".
Re:The Best C++ Interview Question – Ever by Retric on Monday December 07, 2009 @07:11PM (#982607)
IMO, that's not a bad question. C++ is not my thing, but I don't see anyone mentioning.

A) Running the code on another system to double check that the systems memory / OS has not been corrupted. I once wasted ~6 hours on a corrupted production box, so now the new rule is when system call fail double check it's not just the machine.

B) Double check that none of the memory allocation / book keeping memory had been over written, memory had not been freed twice etc.

Honestly, I usually try to do that type of stuff by direct code inspection. If it's failed once I am probably going to need to debug related code in the not to distant future so really understanding what's going on is important, but hack and slash debugging can be fun.

Re:The Best C++ Interview Question – Ever by yan on Monday December 07, 2009 @06:48PM (#982552)
Since we're chiming in on possible solutions, why not do the most general thing first? Check memory usage of the system and process right before segfault, check environment variables that govern malloc behavior (i.e. MALLOC_OPTIONS, OS X-specific ones), check if using (s)brk() system call succeeds right before that, check ulimits.

Then you can either litter break points and start stepping or load the core file in gdb, and start looking closer.

Re:The Best C++ Interview Question – Ever by onewland on Monday December 07, 2009 @06:41PM (#982543)
This feels like kind of a cheap answer, but I know that with Visual Studio you can step into most standard library functions, even if it ends up being at the assembly level; I assume the same is true for gdb.

Is the not asshole answer to be stopping right before the malloc and taking note of memory consumption and determining how much malloc is trying to allocate?

Re:The Best C++ Interview Question – Ever by tptacek on Monday December 07, 2009 @06:57PM (#982576)
Stepping through malloc isn't going to help you much in this case. But most people I've met that have written a lot of C/C++ code have seen this bug at least once.

When you start asking about how much memory malloc is asking for, that's a tip-off in the wrong direction for me too; malloc handles "out of memory" pretty gracefully.

Re:The Best C++ Interview Question – Ever by kscaldef on Monday December 07, 2009 @07:07PM (#982598)
Run the program under valgrind.
Re:The Best C++ Interview Question – Ever by tptacek on Monday December 07, 2009 @07:16PM (#982615)
CHEATER!
Re:The Best C++ Interview Question – Ever by chris100 on Monday December 07, 2009 @06:18PM (#982493)
I'd start logging all the previous calls to malloc/free?
Re:The Best C++ Interview Question – Ever by tptacek on Monday December 07, 2009 @06:57PM (#982578)
You would then be buried under an avalanche of log lines, but then I'd just ask "what are you looking for?".
Re:The Best C++ Interview Question – Ever by aaronblohowiak on Monday December 07, 2009 @05:49PM (#982416)
I believe the following: in evaluating someone's capacity for implementation in one level of abstraction, their understanding of the next level down is a good indicator (but neither necessary nor sufficient precondition) of their understanding of the whole of the system. I'm sorry for the grammatical awkwardness of the preceding sentence, edits welcome.
Re:The Best C++ Interview Question – Ever by dkarl on Monday December 07, 2009 @06:17PM (#982490)
Keep in mind he doesn't care if they answer correctly. I think it's a great question. It's always good to ask a question like this, because C++ programmers often have to deal with these dark corners of the language. Knowing the whole language simply isn't an option. I expect candidates to run through the following questions until they can answer one satisfactorily (hopefully without any prompting):

1. Must it work according to the standard?

2. Is there a reason why it can't work?

3. Are you aware of it working or not working in any particular implementation?

4. Reasoning about how the relevant features might be implemented, can you guess whether it would work?

At some point candidates should express any doubts they feel about the advisability of actually using the technique, or about the conditions under which they would use it, and of course they should explain what further research they would do if the question came up in practice.

In my mind, a candidate can get full marks without actually knowing the answer to the question. I would actually prefer them not knowing, because then I'll learn more about how they think and how well they understand C++.

Re:The Best C++ Interview Question – Ever by j_baker on Monday December 07, 2009 @05:44PM (#982400)
Maybe it's bad at proving how good a C++ programmer someone is, but it's good at seeing how good a programmer someone is.

As the author says, he doesn't expect anyone to get it on their first try. It's more a test of if the candidate can figure the answer out or not. Would you rather have someone who can figure questions like this out on their own or someone who will dismiss this as "trivia"?

Re:The Best C++ Interview Question – Ever by bmm6o on Monday December 07, 2009 @06:10PM (#982470)
Why do posts about interview questions always attract such vehement reactions? The politer replies on the blog itself suggest that the author should work for McDonald's instead of programming. Reddit's discussion isn't much better (predictably). HN can't even have a reasoned discussion about it?
Re:The Best C++ Interview Question – Ever by hristov on Monday December 07, 2009 @06:23PM (#982505)
Because people really do not like being unfairly or capriciously judged on interviews. For many engineers, doing well on an interview is very important for their life, and it is extremely frustrating to have a silly or unfair question.
Re:The Best C++ Interview Question – Ever by bmm6o on Monday December 07, 2009 @11:16PM (#983000)
I get that part. But then in order to justify the level of intensity, doesn't that require someone to concede that they wouldn't have gotten the answer to this one? There seems to be more to it; like "I knew the answer to that one, but he could have easily asked a question that would exclude me!" Is there a question he could have posted that would not have elicited this reaction?
Re:The Best C++ Interview Question – Ever by kscaldef on Monday December 07, 2009 @06:36PM (#982527)
> For many engineers, doing well on an interview is very important for their life, and it is extremely frustrating to have a question they cannot answer.

FTFY

Re:The Best C++ Interview Question – Ever by hristov on Monday December 07, 2009 @06:19PM (#982497)
I agree. I think the correct answer is "it doesn't matter if you can or not, you shouldn't."