Interview Questions

Microsoft Interview Question about C++

Microsoft Interview Questions and Answers


(Continued from previous question...)

Microsoft Interview Question about C++

Question:
Assume you have a program which constantly crashes at some place. Yet, if you insert a 'printf' statement at this place, it magically begins to work. What does this indicate ? How can you trace the problem ?


maybe an answer:
If this is a multithreaded application, I would think it's a race condition. printf can be a little slow at times, so maybe the printf is changing the relative timing of the threads in a multithreaded application.

These types of bugs can be fiendish to track down. I would start by considering the thread that was delayed by the execution of the printf: are there variables that come after the printf, in particular pointers, that are set to a value by a different thread? If so, it's possible that the printf delays its thread enough that other threads are able to do their jobs and initialize variables appropriately, but when the printf is taken away, some of those pointers are dereferenced before other threads can initialize them. A segfault can easily be caused by dereferencing uninitialized pointers.

The best way to stop these bugs is by very careful design of synchronization from the get-go.

(Continued on next question...)

Other Interview Questions