1 Debugging and Syntax Errors in C++. 2 Debugging – a process of finding and fixing bugs (errors...

Post on 17-Jan-2016

240 views 0 download

Transcript of 1 Debugging and Syntax Errors in C++. 2 Debugging – a process of finding and fixing bugs (errors...

1

Debugging and Syntax Errors in C++

2

Debugging – a process of finding and fixing bugs (errors or mistakes) in a computer program.

3

Compilation Errors Program cannot be compiled successfully

Runtime or Logical Errors Program is compiled successfully but runs

incorrectly or crashes

Which kind of errors is more difficult to detect?

Types of Errors

4

Compilation Errors Typical Causes of Compilation Errors Strategy for locating and fixing compilation errors

Runtime/Logical Errors Typical Causes of Runtime or Logical Errors Strategy for locating and fixing logical errors

Debugging programs with IDE Interpreting error messages Adding codes to help detecting bugs

5

Misspelled names e.g.: Mixed up lowercase/uppercase letters

Mismatched parentheses or double quotes i.e.: { } [ ] ( ) "

Omitting punctuation symbols e.g: ; : ,

Not closing a multi-line comment /* ... */

Typical Causes of Compilation Errors

6

/********************************************/ A program that yields compilation errors./*********************************************/#include <iostream>using namespace std;void main( ) { if (strlen(args) > 0) { cout << "Hello, " ; cout << args[0] << "!";} } else cout << "Hello, you!" ;}

1234567891011

Where's the bug?

--------------------Configuration: test - Win32 Debug--------------------Compiling...test.cppH:\bio\exp\test\test\test.cpp(7) : error C2143: syntax error : missing ';' before 'else'H:\bio\exp\test\test\test.cpp(9) : error C2143: syntax error : missing ';' before '}'H:\bio\exp\test\test\test.cpp(9) : error C2143: syntax error : missing ';' before '}'H:\bio\exp\test\test\test.cpp(9) : error C2143: syntax error : missing ';' before '}'Error executing cl.exe.

test.exe - 4 error(s), 0 warning(s)

7

Fact: Many of the errors listed by the compilers are caused by the errors that appear before them.

What should you do? Locate and fix the first error listed by the compiler and

recompile your program (and repeat the process as long as you need to).

There are so many compilation errors!Which error should I fix first?

8

Fact: The bug that corresponds to the compilation error message could appear anywhere before or at the line indicated by the compiler.

What should you do? Inspect the code backward starting at the line indicated by the

compiler (usually just within few lines)

Divide and conquer: Use /* … */ to comment out the block of codes that may contain the bug, then gradually reduce the search range.

I can't find the error at line xxx!

9

Don't panic! Ignore the error message and just inspect the code

around the line indicated by the compiler

Don't give up easily! Try harder for a little longer (After all, it is your code.)

"Google" the error message for possible solutions

Last resort: Ask TAs or teachers

I don't know what these compilation errors mean!

•syntax error before `*'•syntax error before `*'•template argument 1 is invalid•parse error before `)'•ANSI C++ forbids declaration `function' with no type

10

Compilation Errors Typical Causes of Compilation Errors Strategy for locating and fixing compilation errors

Runtime/Logical Errors Typical Causes of Runtime or Logical Errors Strategy for locating and fixing logical errors

Debugging programs with IDE Interpreting error messages Adding codes to help detecting bugs

11

Uninitialized Variables

Forget to create an object using the "new" operator before using the objecte.g.: char * c;

int len = strlen(c);

Miscalculated index rangee.g.: char *s = "abc";

char lastLetter = s[3];

Implemented a "wrong" solution

Typical Causes of Runtime Errors

12

// A program to compute interest#include <iostream>using namespace std;void main( ) { double interestRate, principal, interest; int months;

// Input ...

// Calculation ...

// Output ...}

Principal)12

ateInterest R1(*PrincipalInterest Months

Suppose this program always prints the interest as $0.00.

What could go wrong?

How should you approach to locate the bug?

13

Inspect the value of variables

The behavior or the output of your program depend on the values of some variables.

Basic approaches to find bugs

14

Inspect the value of variables Select some input values which you know what the

output is

Decide which variables to inspect and where to inspect them

Print the value of the variables (to see if they match your expected values)

Basic Approaches To Locate Bugs

15

#include <iostream>using namespace std;void main( ) { double interestRate, principal, interest; int months;

// Input ...

// Calculation ...

// Output ...}

Input: Does not depend on any variable

Calculation: Depends on interestRate, principal, and months

Output: Depends on interest

Decide which variables to inspect

16

#include <iostream>using namespace std;void main( ) { double interestRate, principal, interest; int months;

// Input ... cout << "Before calculation: "; cout << "interestRate = " << interestRate; cout << "principal = " << principal; cout << "months = " << months; // Calculation ... cout << "Before output: "; cout << "interest = " << interest; // Output ...}

Output the value of the dependent variables

17

Sometimes you may only want to debug part of your program because … The rest of the code have already been fully tested

Execution time is long e.g: You only want to test the output part but the

calculation part takes several hours to complete

That's the only part that you write/modify.

Debugging part of program

18

#include <iostream>using namespace std;void main( ) { double interestRate, principal, interest; int months;

// Input ... interestRate = 0.12; // 12% principal = 10000; months = 2; // Expected interest is 201 // Calculation ….

cout <<"interest = " << interest; // Output ... }

Debugging only the "Calculation" part …

Manually set the value of the dependent variables

Check the calculated value against the expected value

19

#include <iostream>using namespace std;void main( ) { double interestRate, principal, interest; int months;

/* // Input ... */ interestRate = 0.12; // 12% principal = 10000; months = 2; // Expected interest is 201 // Calculation …

cout <<"interest = " << interest; /*// Output ...*/ }

Debugging only the "Calculation" part …

Optionally, comment the codes that are not subject to testing

21

Compilation Errors Typical Causes of Compilation Errors Strategy for locating and fixing compilation errors

Runtime/Logical Errors Typical Causes of Runtime or Logical Errors Strategy for locating and fixing logical errors

Debugging programs with IDE

Interpreting error messages Adding codes to help detecting bugs

22

How can IDE help you in debugging your programs?

Visual debugger (easy to use)

Allows you to specify where to pause an executing program

Allows you to inspect the value of the variables when a program is paused

Allows you to trace your program execution

23

Running Program in Debugging Mode

Run Main Project (ctrl+F5) Execute the program in normal mode

Debug Main Project (F5) Execute the program in debugging mode Debugger can only pause a program when you are

running the program in debugging mode

24

Specifying Where To Pause A ProgramA breakpoint typically represents a location (usually a line of code) where an executing program is paused.

To toggle (set or remove) a breakpoint at a line of code, click at the spot in the left grey area next to the line in the editing window.

A small red square indicates that a break point is set at that line.

Multiple breaks points are allowed.

25

Inspecting Value of Variables

When a program is paused, you can view the value of the variables through:

"Local Variables" windowAutomatically shows all the initialized local variables

"Watches" window Shows only the variables added manually by the programmer.

26

Breakpoints

Shortcut to see the breakpoints

Opens the breakpoint window and integrates it along with other windows at the bottom of Visual Studio.NET environment. This window contains information concerning all breakpoints that are currently set.

27

In Between Breakpoints

When you want to continue running your program, you can use

Continue (F5) continue execution until next breakpoint is

encountered

Step As simple as Step Into or Step Over Take one "step" of execution and pause How big is one step?

28

Stepping

Step Over (F10) Execute one line of code and pause

Step Into (F11) If a line of code involves a method call, go into

the method if possible and pause

Otherwise, execute one line of code and pause

Step Out (Shift-F11) Execute the remaining lines of codes in the

current method and pause

29

Other Features

Hexadecimal Display converts all decimal variable values that are in the watch

window, local window, and other windows into hexadecimal values.

Run to cursor (Ctrl-F10) Use the cursor in the editing window as a temporary

breakpoint so execution will pause at the cursor.

Finish Debugger Session (Shift-F5) Stop debugging the program

30

Debug Menu

The Visual Studio.NET Debug menu contains all of the above selections for those that prefer using menus over toolbars.

31

Compilation Errors Typical Causes of Compilation Errors Strategy for locating and fixing compilation errors

Runtime/Logical Errors Typical Causes of Runtime or Logical Errors Strategy for locating and fixing logical errors

Debugging programs with IDE Interpreting error messages Adding codes to help detecting bugs

32

Interpreting error messages

From time to time, you may come across error messages, and error types are:

out-of-memory error String index out of range ….

33

Adding codes to help detecting bugs

You can minimize runtime errors in your program by adding code to check variables against invalid values.

e.g.:// Before calculate interest …

if (month < 0 || principal < 0.0 ||

interestRate < 0.0) {

// Print an error message and maybe

// write code to recover from the error.

}