1 / 17

Writing a Good Program 4. Basic Software Engineering

This article explains the process of debugging a program, focusing on identifying and correcting compile-time and run-time errors. It provides tips to avoid run-time errors and discusses how to debug them using checkpoints and the run-time debugger in Visual Studio.

kbanks
Download Presentation

Writing a Good Program 4. Basic Software Engineering

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Writing a Good Program 4. Basic Software Engineering

  2. 4.2 Debugging a Program

  3. It is often NOT the case that a program can run correctly once it is developed. • Debugging is often a necessary procedure. • Debugging can be divided into the following two stages: • 1. Compile-time debugging (relatively easy) • Correct the errors owing to incorrect use of language syntax or unresolved references. • Run-time debugging (often far more difficult) • Correct the errors owing to incorrect program logic or resource usage. e.g. Memory fault

  4. Compile-time Debugging • Compile-time errors can often be spotted out by the compiler. • The compiler is able to detect the approximate error location, but cannot be exact. • Also, the error messages generated are sometimes irrelevant. • When a series of error messages are found, always try to debug them starting from the first one. • Once the first one is corrected, the rest may be solved as well.

  5. Run-time Debugging Can have no error message • Comparing with compile-time errors, run-time errors are more difficult to locate and correct. • Run-time errors are often caused by wrong programming logic or improper use of resources. • Run-time errors may sometimes lead to incorrect results or even system failure. • Even worst, some run-time errors give no observable problem but gradually damage the system or the database.

  6. How to avoid Run-time Errors? • C++ has embedded many standard features to allow possible run-time errors be detected during compile-time (will be covered throughout this course) • A structured way in program development can help avoid run-time errors • Never develop your program in one step. • Always ensure the skeleton is correct before going forward. Warning Rule of thumb: Try your very best to enable the compiler to detect the errors for you (that is, to convert run-time errors to compile-time errors whenever allowable).

  7. How to Debug Run-time Errors? • The problem of run-time errors can be observed only at run-time. • One has to run the program in order to debug run-time errors. • To debug run-time errors, the first thing that has to be done is to locate the errors. • Achieved by the divide-and-conquer approach. • The simplest way is to set some check-points in the program and to see how many check-points the program can correctly get through.

  8. #include <iostream> using namespace std; int main() { int a, b, c; cout << "Enter 3 numbers: \n"; cin >> a; cout << a << endl; // Checkpoint cin >> b; cout << b << endl; // Checkpoint cin >> c; cout << c << endl; // Checkpoint if (c = (a+b)) { cout << a << endl << b << endl << c; // Checkpoint cout << "c = a+b\n"; } return 0; } • The program on the right was used in Exercise 3.3. • It has a run-time error in line 12. • To correctly locate that errors, four checkpoints are added to show the value of a, b, and c at different parts of the program. The output tells which statements have been executed.

  9. Run-time Debugger of Visual Studio • To help in debugging more complicated run-time errors, Visual Studio has provided a run-time debugger • Allow line-by-line tracing of program codes. • Allow arbitrary breakpoint in a program. • Allow examination of the status of all resources at the breakpoints, such as • Data in memory and • Data value of variables. • Allow monitoring of a particular resource of interest.

  10. Step 1: Build the program of interest Click on the lines of code where you would like to set breakpoints Right-click  Breakpoint  Insert Breakpoint Breakpoints: The moments that you would like to examine the status of the resource allocated to the project.

  11. Step 2: Build it successfully. Start the debugger by clicking Debug  Start Debugging This line of code is to be executed Autos displays variables used in the current line of code and the preceding line of code. Status window: Give the value of all related variables.

  12. Status window: One can also particularly watch the result of an expression by typing it in.

  13. Step Out: Return to the calling function Stop debugging Continue debugging until the next breakpoint Step Over: Execute just the current line Step Into: Like Step Over, but if the current statement is a function, go into this function to debug Check View  Toolbars  Debug if you cannot see the debug toolbar.

  14. Step 3: Step over the codes On stepping over each line of code, the current values of the variables are shown.

  15. Step 4: Find the error. The user inputs 2, 3 and 4 for a, b and c respectively. But after executing the if statement, c changes to 5. Obviously there is a problem in that statement. Using Step Over

  16. Step 5: Fix the error Stop Debugging. Go back to the source and fix the error.

  17. Exercise 4.2 1 #include <iostream> 2 using namespace std; 3 int main() 4 { int CurrentX; 5 int x = 50; 6 CurrentX = x; 7 { 8 int x = 100; 9 CurrentX = x; 10 } 11 CurrentX = x; 12 return 0; 13 } • The Run-time Debugger of Visual Studio can also help us analyze the run-time behavior of our program. • For the program on the right, estimate the values of CurrentX before and after the 6th, 9th, and 11th lines are executed. • Verify your solutions by using the Run-time Debugger.

More Related