Buggin' by Example

wxDev-C++

<  Previous   <           = Home =             > Next >

Example Program to Debug

    We're going to explain the debug process by working through an example.  Although we are limiting our discussion to the gdb debugger, the same example could be used for other debuggers.

    To create our example debug project, go to the File menu and select New and then Project. The new project dialog will be displayed.

New project window

Select the Console Application from the window.  We'll name our project "sampleDebug". You'll notice that a new project will be created with a skeleton C++ code called main.cpp.  Replace the C++ code in main.cpp with the following code:


main.cpp
#include <cstdlib>
#include <iostream>

using namespace std;

float fGlobal = 1234.56;

void test2(int* iTest2a, int iTest2b)
{
    char chTest2 = 'r';
    *iTest2a = iTest2b;
    printf("Finished test2\n");
}

void test()
{
    int iTesta, *iTestb; /* Put a breakpoint here */

    test2(&iTesta, 5);
    printf("iTesta = %d\n", iTesta);

    /* The next lines will cause a runtime error
               since iTestb never gets initialized */
    /*
    test2(iTestb, 3);
    printf("iTestb = %d\n", iTestb);
    */
}

int main(int argc, char *argv[])
{
    float fMain = 3.1415;
    test();
    printf("Press any key to continue...");
    getchar (); /* Pause the program from exiting */
    return EXIT_SUCCESS;

}

    Remember to save the project after you've replaced the contents of main.cpp.

    This sample project will demonstrate the concepts of functions, local and global variables, breakpoints, and backtraces.  Note that lines 24-27 are currently commented.

     /*
    test2(iTestb, 3);
    printf("iTestb = %d\n", iTestb);
    */

These lines will not cause an error during compile-time (i.e. the Mingw gcc compiler will create an executable with no reported compile errors), but it will cause a core dump at runtime due to the use of the uninitialized variable iTestb.

In the next few sections, we'll put our sampleDebug project to good use explaining how to use the debugger...