Compiling and Running a C Program
In this lab, we will be using the command line program gcc to compile programs in C. The simplest way to run gcc is as follows (note that there is no file called program.c given in the lab files, this is just an example for your information).
1 | $ gcc program.c |
This compiles program.c into an executable file named a.out. This file can be run with the following command.
1 | $ ./a.out |
gcc has various command line options which you are encouraged to explore. In this lab, however, we will only be using -o, which is used to specify the name of the executable file that gcc creates. Using -o, you would use the following commands to compile program.c into a program named program, and then run it.
1 | $ gcc -o program program.c |
Exercise 1: Simple C Program
In this exercise, we will see an example of preprocessor macro definitions. Macros can be a messy topic, but in general the way they work is that before a C file is compiled, all macro constant names are replaced exactly with the value they refer to.
In the scope of this exercise, we will be using macro definitions exclusively as global constants. Here we define CONSTANT_NAME to refer to literal_value (an integer literal). Note that there is only a space separating name from value.
1 |
Now, look at the code contained in eccentric.c. We see four different examples of basic C control flow. First compile and run the program to see what it does. Play around with the constant values of the four macro: V0 through V3. See how changing them changes the program output. Modifying only these four values, make the program produce the following output.
1 | $ gcc -o eccentric eccentric.c |
There are actually several different combinations of values that can give this output. You should consider what is the minimum number of different values V0 through V3 can have to give this same output. The maximum is four, when they are all distinct from each other.
Debugger
How do you set a breakpoint which only occurs when a set of conditions is true (e.g. when certain variables are a certain value)?
How do you execute the next line of C code in the program after stopping at a breakpoint?
If the next line of code is a function call, you’ll execute the whole function call at once if you use your answer to #3. How do you tell GDB that you want to debug the code inside the function instead?
How do you resume the program after stopping at a breakpoint?
How can you see the value of a variable (or even an expression like 1+2) in gdb?
How do you configure gdb so it prints the value of a variable after every step?
How do you print a list of all variables and their values in the current function?
How do you exit out of gdb?
Exercise 3: Debugging a buggy C program
You will now use your newly acquired gdb knowledge to debug a short C program! Consider the program ll_equal.c. Compile and run the program, and experiment with it. It will give you the following result:
1 | $ gcc -g -o ll_equal ll_equal.c |
Now, start gdb on the program, following the instructions in exercise 1. Set a breakpoint in the ll_equal() function, and run the program. When the debugger returns at the breakpoint, step through the instructions in the function line by line, and examine the values of the variables. Pay attention to the pointers a and b in the function. Are they always pointed to the right address? Find the bug and fix it.
By debugging we find that during the second function call, there seems to be have a situation that we want b’s value when b is null!This is null pointer reference, which seems to be totally wrong.So we need to determin whether a or b is null, then compare value with each other.
1 |
|
Exercise 4: Pointers and Structures in C
Here’s one to help you in your interviews. In ll_cycle.c, complete the function ll_has_cycle() to implement the following algorithm for checking if a singly-linked list has a cycle.
Start with two pointers at the head of the list. We’ll call the first one tortoise and the second one hare.
Advance hare by two nodes. If this is not possible because of a null pointer, we have found the end of the list, and therefore the list is acyclic.
Advance tortoise by one node. (A null pointer check is unnecessary. Why?)
If tortoise and hare point to the same node, the list is cyclic. Otherwise, go back to step 2.
After you have correctly implemented ll_has_cycle(), the program you get when you compile ll_cycle.c will tell you that ll_has_cycle() agrees with what the program expected it to output.
1 |
|