I have been asked this question today.
When debugging, there is an error. But after you add one printf() line. No error.
What are errors can cause this.
This is an open question. So just let me say what I thought.
My answer was not logical at that time. (such as, maybe some memory error? maybe have concurrency issue?)
So what to hear sth you may have.
Some people may say this is not a good or not a reasonable question. But when we dealing with interview, we have no choice. We need to say what ever logical and make it make sende. 🙂
The concurrency issue is pretty valid. An I/O operation such as
printftakes really long compared to other operations, and this sometimes hides a race condition (the problem doesn’t go away, it just manifests less often – harder to debug).Imagine somebody comes up with this flawed idea:
At this point there’s no telling what will happen. If the thread starts really quick, it will read the memory location before the starting thread has a chance to write it. If however the new thread simply prints “Oh hay, I’m a new thread” before reading the variable, it has a pretty good chance to read valid results. Of course, in 1/10 cases it will fail and it will be a pain to debug.