Now get into
emacs by calling
Compile the program by
Emacs will suggest using
make -k for compilation,
agree by hitting
If you try to run this program, the output you get will be pretty amazing. This program contains 2 errors.
Find the first error by using
lint. In the shell window type
%lint main.c | less
less will show you the first page of its diagnostics.
Use it to locate the first error and correct it. Comment this place
in the program to show what mistake you discovered and how you corrected
it. Do not forget to quote the relevant
lint's error message.
Now, in order to find the second error you need to run
emacs window type
Emacs will inquire how to run gdb, suggesting the beginning of command
gdb. You need to complete the command by typing in
main and hitting
window will split into 2, and gdb will start waiting for your commands.
you will run the program inside the debugger. The program will ask you for input data, give it a short string. After this the program will crash with the favorite message:
Segmentation fault (core dumped)
Besides this message it will give you information in which function and
at which line it crashed. In order to analyze this information you can
give a command
will show you the context of the failure: several lines around the line
where the failure occured. If you want to see the stack of the function
calls that led to this failure, you can use a
Most probably this information will be insuffient for you to determine what an error is. Now, you can use gdb to help you. First, even though you know in which function the failure occurs, this function turns out to be inside of the loop. Thus, your goal is to find out which exactly execution of this function has such a sad ending. You can do it by setting a breakpoint, i.e.
Now the debugger will stop each time you reach this function. At this moment you can use one of the three commands
c to continue the execution.
n to go to next line, stepping over the function calls.
s to go to next line, stepping into the function calls.
If you missed some useful step and want to start anew, do not quit the debugger. Type run and confirm your wish to begin again. This can be useful when you make changes in you program and recompile it. Do not forget to recompile your program after you made the changes.
When you find the mistake, correct it. Comment this place in the
program to show what mistake you discovered and how you corrected
gdb to make sure your program works. Check all
important cases. Now, quit gdb and then emacs.
You need to hand in a script showing the modules you corrected
with the comments mentioned above, the compilation using
The second line represents an empty queue and this case should also be
main and with sample runs of the program with the following data:
The second line represents an empty queue and this case should also be tested.I hope that in your future life as programmers you will master these important tools and they will help you to cut debugging time.
Last updated on March 7, 1996.