Microsoft Interview Question


Country: -




Comment hidden because of low score. Click to expand.
7
of 7 vote

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.

- eugene.yarovoi October 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

yes, this could be the case for multithreaded application.

But it also quite often happens for a program when you go beyond the array bounds or similar things. Because inserting 'printf' changes the data layout in your program. Good explanation can be found here:
bytes. com / topic / c / answers/ 498711-segmentation-fault-if-i-dont-use-printf

- Anonymous October 17, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

come on, give people a break !!! x-(

- anon April 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

Are you seriously from Computer Science "Abhra" ???

- Anonymous October 24, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Perhaps, printf is placed after an if statement that has no braces. E.g.

If (true)
   Printf;
    CrashThisApp();

- Anonymous November 05, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Seen a similar problem before, as Eugene pointed out might be multi threading issue or might be the addition of printf changes the offsets in the text region such that the crashing instruction which might not actually be an instruction is not picked. I encountered this problem when one of the libraries was built with a different macro while my application had that macro on. But Eugene made a good point, the straight forward is that only.

- subramanian.ganapathy86 January 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

it may be a loop issue..i mean single for loop statement is not ended.so here it goes to infinite loop..but if printf statement is dere so the loop ends there.so no crash down.

- abhra October 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

-1: Lack of clarity. How would the single for loop statement result in an infinite loop, and not result in this when the printf is applied? What exactly do you mean? Also, I take it that "crashing" is not "looping forever".

- eugene.yarovoi October 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

printf returns something.. so...

- rohan October 19, 2011 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More