Debugging: Catching Bugs ( I ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.

Slides:



Advertisements
Similar presentations
CS 11 C track: lecture 7 Last week: structs, typedef, linked lists This week: hash tables more on the C preprocessor extern const.
Advertisements

Compilation and Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
Things to Remember When Developing 64-bit Software OOO "Program Verification Systems"
Spring 2005, Gülcihan Özdemir Dağ Lecture 12, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 12 Outline 12.1Introduction.
CS16 Week 2 Part 2 Kyle Dewey. Overview Type coercion and casting More on assignment Pre/post increment/decrement scanf Constants Math library Errors.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
1 Classes with Pointer Data Members (II) Ying Wu Electrical Engineering & Computer Science Northwestern University EECS 230.
The Art of Design Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
Wrap Up and Misc Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
1 Array, Pointer and Reference ( III ) Ying Wu Electrical Engineering & Computer Science Northwestern University ECE230 Lectures.
The Art of Design Ying Wu Electrical Engineering & Computer Science Northwestern University ECE230 Lectures Series.
1 String Library and Stream I/O Ying Wu Electrical Engineering & Computer Science Northwestern University ECE230 Lectures Series.
Ch. 1: Software Development (Read) 5 Phases of Software Life Cycle: Problem Analysis and Specification Design Implementation (Coding) Testing, Execution.
. Compilation / Pointers Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
1 Midterm Review Ying Wu Electrical Engineering & Computer Science Northwestern University EECS230 Lectures Series.
Debugging: Catching Bugs ( II ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 4: Enhancing Your Program.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 17 - The Preprocessor Outline 17.1Introduction 17.2The #include Preprocessor Directive 17.3The.
1 Array, Pointer and Reference ( I ) Ying Wu Electrical Engineering and Computer Science Northwestern University EECS 230 Lectures.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
Introduction To C++ Programming 1.0 Basic C++ Program Structure 2.0 Program Control 3.0 Array And Structures 4.0 Function 5.0 Pointer 6.0 Secure Programming.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
 2007 Pearson Education, Inc. All rights reserved C Preprocessor.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 19 - The Preprocessor Outline 19.1 Introduction 19.2 The #include Preprocessor Directive 19.3.
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering.
Windows Programming Lecture 05. Preprocessor Preprocessor Directives Preprocessor directives are instructions for compiler.
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
1 Lecture 5: Part 1 Searching Arrays Searching Arrays: Linear Search and Binary Search Search array for a key value Linear search  Compare each.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Testing and Debugging Version 1.0. All kinds of things can go wrong when you are developing a program. The compiler discovers syntax errors in your code.
1 Debugging: Catching Bugs ( II ) Ying Wu Electrical Engineering & Computer Science Northwestern University EECS 230 Lectures.
CS 261 – Data Structures Preconditions, Postconditions & Assert.
CSIS 123A Lecture 9 Recursion Glenn Stevenson CSIS 113A MSJC.
DEBUGGING. BUG A software bug is an error, flaw, failure, or fault in a computer program or system that causes it to produce an incorrect or unexpected.
Introduction to Exception Handling and Defensive Programming.
CIS-165 C++ Programming I CIS-165 C++ Programming I Bergen Community College Prof. Faisal Aljamal.
Array, Pointer and Reference ( I ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
CSE 232: C++ debugging in Visual Studio and emacs C++ Debugging (in Visual Studio and emacs) We’ve looked at programs from a text-based mode –Shell commands.
CSE 332: C++ debugging Why Debug a Program? When your program crashes –Finding out where it crashed –Examining program memory at that point When a bug.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessing Lecture 12 April 7, 2005.
Function ( I ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
Software Development Problem Analysis and Specification Design Implementation (Coding) Testing, Execution and Debugging Maintenance.
Topics memory alignment and structures typedef for struct names bitwise & for viewing bits malloc and free (dynamic storage in C) new and delete (dynamic.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessor Midterm Review Lecture 7 Feb 17, 2004.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
CSE 332: C++ Statements C++ Statements In C++ statements are basic units of execution –Each ends with ; (can use expressions to compute values) –Statements.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Recursion.
Variables and memory addresses
Chapter 8 Characters and Strings. Objectives In this chapter, you will learn: –To be able to use the functions of the character handling library ( ctype).
THE PREPROCESSOR
The Preprocessor Directives Introduction Preprocessing – Occurs before program compiled Inclusion of external files Definition of symbolic constants.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
CSE 332: C++ expressions Expressions: Operators and Operands Operators obey arity, associativity, and precedence int result = 2 * 3 + 5; // assigns 11.
Fundamental Programming Fundamental Programming Data Processing and Expressions.
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
1 Building a program in C: Preprocessor, Compilation and Linkage.
1 ENERGY 211 / CME 211 Lecture 14 October 22, 2008.
Software Engineering Algorithms, Compilers, & Lifecycle.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
13 C Preprocessor.
Defensive Programming
Chapter 13 - The Preprocessor
Presentation transcript:

Debugging: Catching Bugs ( I ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series

A Little Challenged? Keys in programming –Your solution  design –Language syntax  code –Debugging skills  working programs –Never stop improving  good programs The role of the compiler –Very very little: acting like a grammar checker Your role in programming –You need to have a solution –You need to make sure what you code is what you want –You need to make sure there is no bugs in your code –Overwhelmed? Don’t panic! Life is not as bad as you may think.

Outline Introduction –What is debugging –Why do we need debugging –What do you need to have A simple way –testing a black box –printing out intermediate results Assert yourself –assert( ) Stepping through your code (see next lecture) –Debugging tools

What is a bug? Syntax errors are easy to fix –Once you’ve understood all the syntax But a run-time failure is most likely caused by –Logic bugs –Overflow and underflow bugs –Data conversion bugs –Wild pointer bugs –Garbage memory bugs –…

What is debugging? Always make sure you know what you are expecting from the code If your program did not output what you expected, tract it down to the exact point You will find the bug! Go ahead and fix it. your code inputoutput Debugging

Why? The complier will never catch bugs –The complier only check syntax errors –It will never check if your code would fulfill your tasks. You are on you own to –make your program correct –make your program robust –make sure what is really going on in your code There is no magic in programming

What do you need? Deep understanding of the language itself Fundamentals on low-level programming Debugging tools Good coding style Right attitude

// Binary search int binarySearch( const int b[], int searchKey, int low, int high, int size ) { int middle; while ( low <= high ) { middle = ( low + high ) / 2; if ( searchKey == b[ middle ] ) // match return middle; else if ( searchKey < b[ middle ] ) high = middle - 1; // search low end of array else low = middle + 1; // search high end of array } return -1; // searchKey not found } print_array(b); cout << key << low << high << size << endl; cout << low << middle << high << endl; print_array(b, low, high); cout << low << middle << high << endl; print_array(b, low, high);

A Tale of Two Versions Release version –Clean –Fast Debug version –Contains many debug information –Big and slow –Using macro #ifdef, for example #ifdef DEBUG // print_out_stuff … #endif Maintain both versions for your program

// Binary search int binarySearch( const int b[], int searchKey, int low, int high, int size ) { int middle; while ( low <= high ) { middle = ( low + high ) / 2; if ( searchKey == b[ middle ] ) // match return middle; else if ( searchKey < b[ middle ] ) high = middle - 1; // search low end of array else low = middle + 1; // search high end of array } return -1; // searchKey not found } #ifdef DEBUG print_array(b); cout << key << low << high << size << endl; #endif #ifdef DEBUG cout << low << middle << high << endl; print_array( b, low, high ); #endif #define DEBUG

Introducing assert assert is a macro defined in Functionality –Test the value of an expression –If the value of the exp is 0, then assert prints an error message call function abort() (which is defined in ) –If not, do nothing To cancel all assertion, use #define NDEBUG Some symbolic constant –Line number of current code __LINE__ –Name of current source file __FILE__

Assert Yourself Do not wait for bugs to happen Use startup checks Use assertions –To validate function arguments –To detect impossible conditions –To catch illegal uses of undefined behavior

// Binary search int binarySearch( const int b[], int searchKey, int low, int high, int size ) { int middle; while ( low <= high ) { middle = ( low + high ) / 2; if ( searchKey == b[ middle ] ) // match return middle; else if ( searchKey < b[ middle ] ) high = middle - 1; // search low end of array else low = middle + 1; // search high end of array } return -1; // searchKey not found } assert ( b != NULL && size > 0 ); assert ( low >= 0 && high <= size-1 ); #include assert ( low <= high );

typedef unsigned char byte; // memcpy – copy a nonoverlapping memory block void *memcopy(void *pTo, void *pFrom, size_t size) { byte *pbTo = (byte *)pTo; byte *pbFrom = (byte *)pFrom; while ( size-- > 0) *pbTo++ = *pbFrom++; return (pTo); } assert ( pTo != NULL && pFrom != NULL); assert ( pbTo >= pbFrom + size || pbFrom >= pbTo + size ); pFrompTo pbFrompbTopbFrompbTo pFrompTosize = 10 ?