debugging

The Easiest Programming Bug I Spent Way Too Much Time Trying to Solve

I ran across a question on Quora: What's the easiest programming bug you spent way too much time trying to solve?.

Here is my answer to that question. Some time during my first few years of programming, I wrote code similar to this little snippet and expected it to print "FOO", but instead it prints "None of the above". It is written in C, but should be comprehensible to anyone who knows a language with similar syntax (C++, Java, JavaScript, C#, etc.). It took me about a half day of staring and single-stepping in a debugger to figure out what was wrong. When I looked at the disassembly listing, I was convinced I'd found a bug in the C compiler. Maybe you can figure it out faster than I could.

#include <stdio.h>

int main(int argc, char *argv[])
{
    const int FOO = 1;
    const int BAR = 2;

    int n = FOO;

    switch (n)
    {
        FOO:
            printf("FOO");
            break;

        BAR:
            printf("BAR");
            break;

        default:
            printf("None of the above");
            break;
    }

    return 0;
}

Setting Up for Use of Microsoft Symbol Server

When debugging native Win32 code, it is useful to have the debug symbols for all of Microsoft's DLLs. The easiest way to set this up is to just set an environment variable before starting Visual Studio (or other Microsoft debugging tools):

set _NT_SYMBOL_PATH=srv*c:\symbols*http://msdl.microsoft.com/download/symbols

The first time you run the debugger after setting this, it will take some time to start as it downloads symbol files from the Internet into your local symbol cache, but it will be faster after that. Whenever you update your system with patches or service packs, the new symbols will automatically be downloaded the next time you debug.

For more information, see these pages:

(This post is really for my own benefit. I have trouble finding this information whenever I set up a new development workstation, so I'm putting it somewhere I'll know to look.)

Syndicate content