Errors and Debugging Techniques Errors Generally expected result
Errors and Debugging Techniques
Errors • Generally, expected result does not match actual result • Four kinds of errors are: • Syntax errors • Linker errors • Runtime errors • Logic errors
Errors • Four kinds of errors are: • Syntax errors • Linker errors • Runtime errors • Logic errors
Syntax Error • A “sentence” was formed that does not exist in the language • For example, “Be an awesome program” isn’t valid in C • A reference to an undeclared variable. • Incorrect punctuations.
Syntax Error • Easiest to correct • Compiler will not allow it • *Usually* it will say where it is exactly
On Syntax Errors • . . . sometimes the compiler is really bad at figuring out where the error is #include <stdio. h> int main() { printf( "moo" ) printf( "cow" ); return 0; }
Reality #include <stdio. h> int main() { printf( "moo" ) printf( "cow" ); return 0; } • Missing semicolon at line 4
GCC #include <stdio. h> int main() { printf( "moo" ) printf( "cow" ); return 0; } syntax. c: In function ‘main’: syntax. c: 5: error: expected ‘; ’ before ‘printf’
#include <stdio. h> include <math. h> main() { float p , r , n, f; printf(“Enter valuse for P , R and N”); scanf(“%f”, &p); printf(“Enter value for R ); scanf(“%f”, &r); printf(“Enter value for N”); scanf(“%f”, &n) i = r/100; f=(p*r*n; /* write output /* printf(“Simple interest is %f”, f); }
Errors in program • # • “ • ; • Improper brackets • i is not declared • /* /* comments
Errors • Four kinds of errors are : • Syntax errors • Linker errors • Runtime errors • Logic errors
Recall Linking 1: something. From. Here(); 2: something. From. Elsewhere(); 3: something. Else. From. Here(); something. From. Here something. Else. From. Here something. From. Elsewhere
Recall Linking something. From. Elsewhere something. From. Here something. Else. From. Here
Linker Errors • What if something. From. Elsewhere is nowhere to be found? • Missing a piece • Cannot make the executable
Example int something(); int main() { something(); return 0; } • int something(); tells the compiler that something exists somewhere, but it does not actually give something
• You misspell the name of a function (or method) when you declare, define or call it: void Foo(); int main() { Foo(); return 0; } void foo() { // do something }
Errors • Four kinds of errors are : • Syntax errors • Linker errors • Runtime errors • Logic errors
Runtime Errors • Error that occurs while the code is running • Compilation and linking must have succeeded to get to this point
Examples • • Attempting to calculate sqaure root of a negative number. Divide by zero (especially for integers!) unsigned int x = 5 / 0; • Wrong printf placeholder printf( “%s”, 57 );
Errors • Four kinds of errors are : • Syntax errors • Linker errors • Runtime errors • Logic errors
Logic Errors • It works! • . . . but it doesn’t do what you wanted • Like getting the wrong order at a restaurant
Examples • Transcribed an equation incorrectly • Using the wrong variable • Lack of understanding of problem
Logic Errors • By far, the most difficult to debug • It might be done almost correctly • This is why testing is so important!
Debugging Techniques • Ad-Hoc Insert printf statements to track control flow and values Code explicitly checks for values out of expected range, etc. Advantage: No special debugging tools needed Disadvantages: Requires intimate knowledge of code and expected values Frequent re-compile and execute cycles Inserted code can be buggy • • • Source-Level Debugger Examine and set variable values Tracing, breakpoints, single-stepping on source-code statements • •
Source-Level Debugging Techniques • Breakpoints Stop when a particular statement is reached Stop at entry or exit of a function Conditional breakpoints: Stop if a variable is equal to a specific value, etc. Watchpoints: Stop when a variable is set to a specific value Single-Stepping Execute one statement at a time Step "into" or step "over" function calls Step into: next statement is first inside function call Step over: execute function without stopping Step out: finish executing current function and stop on exit • • •
- Slides: 25