Lecture 07 - Debugging
Lecture 07 - Debugging
In this lecture
• What is debugging
• Most Common Type of errors
• Process of debugging
• Examples
• Further readings
• Exercises
What is Debugging
Debugging is the process of finding compile time and run
time errors in the code. Compile time errors occur due to
misuse of C language constructs. Most of the compile time
errors can be overcome with careful attention to language
syntax and use. Finding syntax errors is the first step in
a debugging process. A typical C project that contains
main.c, lib.h and lib.c can be compiled at once with
Run time errors are the hardest to deal with. Run time
errors can cause program to crash, or give you the
incorrect output. First step in dealing with run time
errors is to remove reasons for program to crash. Some of
the most common errors of program crash are, dereferencing
memory that has not been allocated, freeing memory that was
already freed once, opening a file that does not exists,
reading incorrect or non-existent data or processing
command line arguments that have not been provided.
• Missing/misplaced semicolons
The most common type of error in the early stages of
development. Each statement in a program must be
separated by a semi colon. But avoid putting a semi-
colon at the end of a loop declaration. This error can
be corrected easily as compiler will provide enough
clues as to the line where semi colons are missing or
misplaced
Run time errors are much more serious. There are many
errors to look out for. Among some of them are
• Array index out of bounds
This error is occurred when an out of bounds array
index is referenced. In most cases a user can
unintentionally assign A[n] = something to an array
of size n. But problem comes when A[n] is
dereferenced.
> Why?!
Why?!: Command not found
r(un) [arglist]
Runs your program in GDB with optional argument list
b(reak) [file:]function/line
Puts a breakpoint in that will stop your program when
it is reached
n(ext)
When stopped, runs the next line of code, stepping
over functions
s(tep)
When stopped, runs the next line of code, stepping
into functions
q(uit)
Exits GDB
print expr
Prints out the given expression
display var
Displays the given variable at every step of execution
l(ist)
Lists source code
help [command]
Gives you help with a specified command
bt
Gives a backtrace (Lists the call stack with variables
passed in)
Example 2
Consider the following program
Example 3
Consider the following program
return EXIT_SUCCESS;
}
Further Readings
[1] http://www.cs.cmu.edu/~thoffman/S09-15123/Chapter-3/Chapter-
3.html#CHAP_3.4
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
foo(int*** array){
int** arrayint = (int**)malloc(2*n*sizeof(int*));
for (i=0;i<n;i++)
arrayint[i] = (*array)[i];
free(*array);
array = &arrayint;
}