These lab exercises are intended to show you how to run a C program on the EECS instructional computers, and introduce you to the C debugger gdb. You will gain some practical experience using the debugger while exploring the structure of C strings.
You need to have an EECS instructional account (cs61c-*) in order to do your lab work. Most importantly, you will need to log in to this account in order to enroll in the class. Don't forget to change your password (passwd) once you have logged in.
Copy the contents of ~cs61c/labs/01 to a suitable location in your home directory.
$ mkdir ~/lab $ cp -R ~cs61c/labs/01/ ~/lab
Fill in the blank in the following C program, also in output0.c, so that its output is a line containing 0. Don't change anything else in the program.
#include <stdio.h> int main ( ) { int n; n = _____; printf ("%c\n", n); return 0; }
To verify your solution, compile it and run the resulting binary:
$ gcc -o output0 output0.c $ ./output0 0
Explain to your TA the change you made to output0 |
For this exercise, you will find the GDB reference card useful. Compile your solution to exercise 1 with the "-g" flag:
$ gcc -g -o output0 output0.cThis causes gcc to store information in the executable program for gdb to make sense of it. There are two ways to start the debugger:
Type help from within gdb to find out the commands to do these things, or use the reference card.
Learning these commands will prove useful for the rest of this lab, and your C programming career in general. Create a text file containing answers to the following questions.
Set the breakpoint at main, and show your TA how you run up to that breakpoint. | |
Show your TA your text document containing the additional gdb commands. |
You will now use your newly acquired gdb knowledge to debug a short C program!
Consider the C program appendTest.c. Compile and run the program, and experiment with it. Try appending a few strings, and notice that it does not always produce the correct result. (Press Ctrl-C to exit).
Now, start gdb on the program, following the instructions in exercise two. Set a breakpoint in the append function, and run the program. When the debugger returns at the breakpoint, step through the instructions in the append function line by line, and examine the values of the variables. Pay attention to s1 and s2 in the append function. Are they correct? Why is this a bug?
Hint: How does C represent strings? Fix this bug.
Explain the inconsistent behavior of the buggy program, and explain your fix. |