CS 241 Data Organization using C
Lab 7: Pointers, Structs, and GDB


October 18, 2018

The purpose of lab today is to get you familiar with how to debug pointers in GDB.

1 Getting Started

Download the https://www.cs.unm.edu/~vasek/cs241/lab/bintree.c file. We’re going to be debugging this code today.

Compile the code using the normal flags as well as the flag needed for GDB to work. (Remember this from lab 4? https://www.cs.unm.edu/~vasek/cs241/lab/lab4.html)

2 GDB with command line args

run a b c will run your code, piping a b c as command line arguments.

Try compiling and using GDB to run the bintree.c file. You need to pass in a sequence of numbers to the program.

3 GDB with structs and pointers

The print command can not only tell you the current value of a variable, but also the value of different parts of a struct or the value that a pointer points to. The syntax is similar to that in c.

If you’re in the middle of the program you can do the following:

You can use the commands command. commands 1 will allow you to write some code to execute each time you hit the first breakpoint you defined.

4 To do during lab

You will need to write a number of commands in GDB, save them into a textfile called “gdbpointers.txt”.

Each of the following is worth some points towards your three points. For each, you will need to write the commands needed to produce your answer:

0.5
What address is tmp stored in memory the first time you run makenode?
0.5
What address is the structure that tmp points to stored in memory the first time you run makenode, after malloc has executed?
0.5
After your tree has one node in it, write a command to print out the node.
0.5
After your tree has two nodes in it, write a command to print out the value stored in the node’s left and/or right subtree.
1
There is a bug in the program. Write the sequence of gdb commands needed to find it.

5 Turning in your program

It’s ok not to finish this assignment – get through as much as you can.

Zip up your gdbpointers.txt file into a zip file entitled yourCSUsername.zip. Then drop the zip file into the dropbox under the lab-07 folder..