All pastes #743887 Raw Edit

Someone

public text v1 · immutable
·published 2007-10-20 21:22 UTC
rendered paste body
Reading the tape
----------------


When your debugger fails you, like for example you can only see a problem when compiling without debug information enabled (and yes, it does happen), you will have to resort to the ancient art of "reading the tape". The idea is to print meaningful output messages at strategic locations to work out exactly where the program's execution deviates from your idea of what it should be doing.

The following example demonstrates that reading the tape is required, and although the method is inflexible and not very efficient - it does get results.

<pre>

class LightSwitch
{
	public: 
		bool isOn() { return IsOn; }
		void toggle() { IsOn = !IsOn; }
	private:
		bool IsOn;
};

int main(void)
{
	// we make a new light switch
	LightSwitch *l = new LightSwitch();

	// Turn it on
	if (l->isOn() != true)
		l->toggle();

	// It's still not on?!
	if (l->isOn() != true)
		printf("The light will not turn on\n");
	else
		printf("The light is on\n");
}
</pre>

<answer>In this case, the light was never initialized. In debug mode the memory where the variables live is blanked before use, so you don't see the problem (IsOn=false=0). When running without debug mode the variable is any old random number, which means it isn't true or false but usually somewhere in between. When toggle is called </answer>