CS2106 Laboratory 2: C Pointers and Memory Management
CS2106 Laboratory 2: C Pointers and Memory Management
School of Computing
CS2106 Laboratory 2 Semester 1 10/11
Deadline
3 September, 2010, Friday, 11:59pm.
Platform
This exercise requires the Linux platform. You may use the Linux machines in the OS Lab. If
you want to use your own Linux machine, make sure that you have valgrind installed besides
make, gcc, and gdb.
Objective
In this exercise, you will program in C using pointers. You will get a chance to practice using
gdb. You will also learn about make, assert, and valgrind.
You will implement one of the most basic data structure, a queue, using a linked list, with five
functions, queue create, queue delete, queue enq, queue deq, and queue is empty. Queue
is a common data structure used in operating system. A quick review of linked list and queue
from CS1102 might be useful for some of you.
Skeleton Code
A skeleton code has been given to you. You can download the skeleton code with the command:
wget http://www.comp.nus.edu.sg/~ooiwt/cs2106/lab02.tar.gz
You should see a directory called lab02-A000000X. Now, rename this directory (using mv
command) to replace the suffix A000000X with your matriculation number2 .
For simplicity, we will still refer to lab02-A000000X in the rest of this lab. You must replace
the suffix A000000X with your matriculation number.
Under the directory lab02-A000000X, you will see four files:
• queue.h: a header file that declares the structure of queue and functions that operate on
queue.
• queue test.c: a test program that illustrates how the queue data structure is used as
well as tests the correctness of the implementation.
1
man gzip and man tar to find out what these options mean if you are interested
2
Hint: you can use TAB to autocomplete in bash shell
Compiling and Running
To compile, type
make
in your shell. The given skeleton code should compile, producing the executable file queue test.
Type
queue_test
to run the test program. You should received a segmentation fault error due to bugs in
queue enq.
Makefile
Makefile is a text file that contains compilation instructions with information about dependen-
cies among the files. It is the default input to the make command. The nice thing about make
is that, it checks for dependencies for you and only modified files are re-compiled. For instance,
if you change queue test.c, then queue.c is not re-compiled when you run make. Interested
students can google and learn how to write your own Makefile yourself as we will not cover this
in CS2106.
assert
There are many calls to the assert macro in queue test.c assert aborts the execution if the
given statement is false, and is extremely handy in checking for invariants in a program.
Your Task
In queue.c, a buggy queue enq has been given to you. This function will cause a segmentation
fault when you run queue test. You should identify the bug and fix it. The gdb debugger
might be helpful to you here.
An empty queue deq and queue delete has been given to you. You should complete these
two functions according to the specification given in the code. Besides ensuring correct im-
plementation of the two functions, you should use free to deallocate any allocated memory
properly.
Page 2
Using valgrind
valgrind is a tool that is useful to track down memory errors (invalid memory access, memory
leaks etc.). To get full marks for this exercise, not only should your solution pass all test defined
in queue test.c, it should run in valgrind without any memory leaks or other memory errors.
To run your executable in valgrind, type:
http://www.cprogramming.com/debugging/valgrind.html
Submission
In the parent directory of lab02-A000000X, run
Marking Scheme
You get marks for the following:
• 5 marks for passing all tests in queue test without assertion error.
Partial credit will be given appropriately if your solution is partially correct or partially leaky.
You get mark deduction for the following:
THE END
3
If you make multiple IVLE submissions, IVLE will rename the files – we won’t penalize you for that.
Page 3