In a recent interview, I was asked the following question:
You have an error in your program, after trying to debug it by inserting expressions such as printf, console.log, System.out.println, echo, etc., the error will disappear. How can this happen?
I answered the following answers:
- You have something with side effects in your print instruction, for example:
System.out.println(foo.somethingWithSideEffects()) - Adding printf changes the memory layout of the program, so it can span neighboring memory and prevent crashes
- Undefined Behavior in native code (e.g. uninitialized values, buffer overflows, sequence points, etc.)
The interviewer said that this is not the only way this can happen, and I could not think of any other ways to just add printf, etc., to “fix” the error in the program.
What else could lead to this?
source
share