Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good...

17
1 Computer Programming and Basic Software Engineering 4. Basic Software Engineering Writing a Good Program 4. Basic Software Engineering

Transcript of Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good...

1

Computer Programming and Basic Software Engineering

4. Basic Software Engineering

Writing a Good Program 4. Basic Software Engineering

2

Computer Programming and Basic Software Engineering

4. Basic Software Engineering

4.2 Debugging a Program

3

Computer Programming and Basic Software Engineering

4. Basic Software Engineering

• 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.

2. Run-time debugging (often far more difficult)

Correct the errors owing to incorrect program logic or resource usage.

e.g. Memory fault

4

Computer Programming and Basic Software Engineering

4. Basic Software Engineering

• 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.

Compile-time Debugging

5

Computer Programming and Basic Software Engineering

4. Basic Software Engineering

Run-time Debugging

• 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.

Can have no error message

6

Computer Programming and Basic Software Engineering

4. Basic Software Engineering

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.

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).

Warning

7

Computer Programming and Basic Software Engineering

4. Basic Software Engineering

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

Computer Programming and Basic Software Engineering

4. Basic Software Engineering#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

Computer Programming and Basic Software Engineering

4. Basic Software Engineering

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

Computer Programming and Basic Software Engineering

4. Basic Software Engineering

Step 1:Build the program of interest

Click on the lines of code where you would like to set breakpoints

Breakpoints:The moments that you would like to examine the status of the resource allocated to the project.

Breakpoints:The moments that you would like to examine the status of the resource allocated to the project.

Right-click Breakpoint Insert Breakpoint

11

Computer Programming and Basic Software Engineering

4. Basic Software Engineering

Step 2:Build it successfully. Start the debugger by clicking Debug Start Debugging

Status window: Give the value of all related variables.

Status window: Give the value of all related variables.

This line of code is to be executed

This line of code is to be executed Autos displays variables used in

the current line of code and the preceding line of code.

12

Computer Programming and Basic Software Engineering

4. Basic Software Engineering

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

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

13

Computer Programming and Basic Software Engineering

4. Basic Software Engineering

Stop debuggingStop debugging

Continue debugging until the next breakpoint

Continue debugging until the next breakpoint

Step Over:Execute just the current line

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

Step Into: Like Step Over, but if the current statement is a function, go into this function to debug

Step Out:Return to the calling function

Step Out:Return to the calling function

Check View Toolbars Debug if you cannot see the debug toolbar.

14

Computer Programming and Basic Software Engineering

4. Basic Software Engineering

On stepping over each line of code, the current values of the variables are shown.

On stepping over each line of code, the current values of the variables are shown.

Step 3: Step over the codes

15

Computer Programming and Basic Software Engineering

4. Basic Software Engineering

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.

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.

Step 4: Find the error.

Using Step Over

16

Computer Programming and Basic Software Engineering

4. Basic Software Engineering

Stop Debugging. Go back to the source and fix the error.

Stop Debugging. Go back to the source and fix the error.

Step 5: Fix the error

17

Computer Programming and Basic Software Engineering

4. Basic Software Engineering

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.