Visual Studio .NET 2005 Debugger Part I


In previous lectures when a program was run the menu option: Debug/Start without Debugging was selected. When this option was chosen, the Debugger did not activate. The question is: What is the Debugger and what does it do? For the novice, the name may seem to imply that this option debugs the program i.e. remove any bugs. However, this is not the purpose of the Debugger. It is used to help programmers remove any bugs. The Debugger does this by providing a visual display of the activities of the program. It can be used to show each step and to show what is being stored in each memory location used by the program.

The debugger has four main options:

  • Establish break points where the program will stop

  • Step through the program one line at a time

  • View each memory location being used by the program

  • Examine memory when various functions are called

In this discussion the first three of these will be examined. The fourth option will be discussed later after functions have been discussed.

Link to EXAMPLE2C.CPP and load it into Visual Studio .NET. What you will see is the following program:

image from book

 // program_id exampl2c.cpp // // program_description This program uses the concept of // namespace to simplify the coding. // #include<iostream> using namespace std; int main() {    const double factor = 1.5;    double payRate;    double regularHours;    double overtimeHours;    double totalPay;    double regularPay;    double overtimePay;    cout << endl << "What is the employee's regular pay rate?";    cin >> payRate;    cout << endl << "What is the employee's regular hours?";    cin >> regularHours;    cout << endl << "What is the employee's overtime hours?";    cin >> overtimeHours;    regularPay = regularHours * payRate;    overtimePay = overtimeHours * payRate * factor;    totalPay = regularPay + overtimePay;    cout << endl << "The regular hours was " << regularHours;    cout << endl << "The overtime hours was " << overtimeHours;    cout << endl << "The regular pay was " << regularPay;    cout << endl << "The overtime pay was "<< overtimePay;    cout << endl << "The total pay was "<< totalPay << endl << endl;    return 1; } 

image from book

Using your mouse, click in the bar to the left of the program opposite the three lines as indicated by the dots in the graphic below. What you should see is the following:

image from book

Next compile and then click on the Debug/Start option on the menu bar. When you do, you should see something like the following:

image from book

Notice that the program stopped at the line:

image from book

 const double factor = 1.5; 

image from book

Note that the dot to the left of this line now has an arrow pointing toward the line. This line defines the variable: factor to be a constant double. Now look below the program at a region of the screen labeled Autos. It should appear like the following graphic:

image from book

In this table the Name of the variable factor is listed on the left and its Type on the right that is listed as a const double. In the middle of the table is listed the Value of factor. You would expect the value to be listed as 1.5 but the Debugger stopped at the definition of the variable before it was initialized. As a result, the variable factor is loaded with a garbage value. This is what will happen everytime the Debugger stops or passes a variable definition. It shows that there is garbage in variables prior to initialization.

Next click the menu option: Debug/Continue. (Note that this can be accomplished by pressing the F5 key.) The program has passed over the definitions for six double variable and stops at the next dot which is the statement: cin >> payrate. Look one more time at the table Autos at the bottom of the screen. They should look like the following:

image from book

Observer that the six variables just defined are listed with garbage values that on my system were all the same value.

Press F5 (i.e. Debug/Continue) and enter some values for the requested payRate, regularHours and overtimeHours. Once you have done this, the program will stop at the statement:

image from book

 return 1; 

image from book

Notice in the Autos table at the bottom of the screen, the variable: totalPay now has a value depending on what you entered for the previously requested values. Pressing the F5 key one more time terminates the program. Therefore using this technique, it is possible to trace the values of each of the variables during the execution of the program.

As noted in the previous discussion of Visual Studio .NET, the executable example2C.exe of this program would be stored in the folder Debug. When I viewed the file size on my computer, it was 48 kilobytes. This is not the file you would submit as your final program to your client. This file contains additional instructions for Visual Studio .NET and the Debugger to manage the executable as was done in the example above. When you are ready to submit the Release version, you need to change the Compiler Configuration. To prepare a release version of your program select the menu option: Build/Configuration Manager as seen the following graphic:

image from book

When this option is selected, the following screen will appear:

image from book

Click on the list box under Active Solution Configuration and select Release as the option of choice. When this is done, the following screen should appear:

image from book

To complete this process, click the Close button and recompile. When I did this on my computer, a new folder was created called: release. In this folder was a collection of files including the executable example2C.exe. When viewing the file size on my system, this file was 9 kilobytes. As can be noticed, this has reduced the size significantly. As you create programs in this course, create a release version of the program and notice the size difference between the Debug executable and the Release executable.




Intermediate Business Programming with C++
Intermediate Business Programming with C++
ISBN: 738453099
EAN: N/A
Year: 2007
Pages: 142

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net