-1

I was once asked during an interview the following question, and I've still never been quite clear on the answer. I was wondering if anyone knew where I could learn more, googling hasn't been much help:

Say you have a program that you'd like to test. You add a single log statement and suddenly, the program which was producing expected output stops producing expected output. What could have happened?

1
  • print("the counter is at " + i++) \\ lol lol lol Commented Oct 14, 2014 at 7:55

2 Answers 2

3

Aha. I've actually had this happen.

Let's consider a program that has a bug that is munging the stack. If you introduce a log or print statement, the call to the log may shift the stack enough to cause it to change behaviour.

It's an interesting problem to think how to show an example. Probably easiest to do it with a bad format in a printf...

okay, in outline at least an example will look like this.

int parent(){ ... printf("%s\n", itoa(child()));

int child(){
    int num;
    scanf("%d%d", num);  /* notice the format; scanf is going to eat more of the
                           * stack than it should.
                           */
    return num;            /* but this return may unwind the stack successfully. */
}

Your case would happen if you insert a printf() just before the return.

Sign up to request clarification or add additional context in comments.

6 Comments

"minging the stack"? Ming is not a verb and if it were, in this context I don't see what any of it has to do with the Ming Dynasty in China.
@Colin Minging is British slang for ugly, so I read this as uglifying :-)
Sadly, Michael, it was a good attempt at a save that I just ruined by correcting my own typo. I'm trying to get somewhere with "Minging the merciless" but it's too late at night....
Definitions of munging on the Web: * Mung is computer jargon for "to make repeated changes which individually may be reversible, yet which ultimately result in an unintentional ... en.wikipedia.org/wiki/Munging * Munge - In computing, the term munge [mʌndʒ] means to create a strong, secure password through character substitution. "Munge" is sometimes backronymmed as Modify Until Not Guessed Easily. ... en.wikipedia.org/wiki/Munge
|
2

Your program might have race conditions between concurrent threads, so any change to the timing may change the program behaviour.

Usually this is the other way round, which is much worse (the so called Heisenbug): Your program misbehaves and you want to debug it by adding log output. But the log output makes the problem go away, so it becomes very hard to diagnose.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.