CPSC 213: Assignment 5

Due Mon Oct 22, 6pm

Goal

In this assignment you will become familiar with C, including compiling, running, and debugging programs written in it. In addition, you will become more mindful of endianness issues and gain experience in understanding how pointer arithmetic works, as well as the correct use of dynamic allocation.

Exercise 1: Endianness and Basic C

Download the package for this assignment and extract it. One of the files contained within, a5_1.c, is a simple program that demonstrates endianness differences depending on the machine it's run on. SSH into one of the x86 undergrad machines (annacis, bowen, deas, lulu) and compile it with the command

gcc -m32 -g -o a5_1 a5_1.c
(the -m32 option forces gcc to use 32-bit mode, which will be important below) then run it with ./a5_1 in the directory where the binary resides, and observe its output. Your task is to modify a5_1.c so that it:

Do NOT hardcode the value of x as a string to accomplish the above. It should remain a numerical value like it is in the original code. Your modified program should produce output of the following form:

The value of x is ________, or 0x________ in hexadecimal
The value of ipx is ________ and the value of *ipx is ________
The value of cpx is ________
The value of cpx[0] is ___
The value of cpx[1] is ___
The value of cpx[2] is ___
The value of cpx[3] is ___

Use gdb to step through the program and familiarise yourself with how to trace execution, set breakpoints, and examine variables using it. Read the gdb tutorial for a quick introduction on how to use gdb.

Now SSH into one of the SPARC machines (galiano, gambier) and run the program again, after recompiling it with the same command as above (if you try to run an x86 binary on a SPARC system or vice-versa, you will receive an error saying the architecture is not compatible). You should see a difference in the output; that corresponds to the endianness difference, as SPARC is big endian, whereas x86 is little. Save the output of your program when run on little and big-endian machines in files named a5_1le.txt and a5_1be.txt respectively for handing in, and in each, give the calculation that shows, for that particular endianness representation, how the sequence of values printed out by the program represents x.

Exercise 2: Pointer Arithmetic

a5_2.c in the provided materials contains a program that demonstrates various uses of pointer arithmetic. Follow the instructions in the comments to replace the various values with those derived from your student number. Compile and run the program on one of the Linux x86 machines in 32-bit mode and answer the questions in the comments. Show your work to receive credit: for example, just saying that a[5] is at address 497710 without any explanation will receive a mark of 0; saying that a starts at 497550 and that each element of a is 32 bytes, and thus a[5] is at 497550 + 32*5 = 497710, will receive full credit. Although you are encouraged to verify your answers by compiling and running, the goal is for you to understand why and not just to modify the program to give you an answer. As with the first part, use gdb to help you examine memory. For handing in, submit both your modified a5_2.c and answers to the questions in a file named a5_2.txt .

Exercise 3: Dynamic Allocation

The file a5_3.c contains an implementation of a queue of strings, along with a simple interface that allows you to add elements at one end ("enqueue"), and remove elements from the other ("dequeue"), and inspect the contents of the queue. It also contains several memory allocation bugs. Your task is to find and fix these bugs, without introducing any others. Add comments indicating which portions of the code you changed, with a brief description of why that change was made/what type of bug was fixed. Note that fixing some of the bugs may require changing the queue interface. Once again, gdb will be useful for this exercise. Your corrected version should work on both the Linux and Solaris undergrad machines.

Hints

Provided Materials

Handing In

Use the handin program. The assignment directory is a5. Please hand in exactly and only the following files with the specified names.

  1. README.txt that contains
  2. a5_1.c containing your modified version of the provided a5_1.c, as described above
  3. a5_1le.txt containing the output of your modified a5_1 program when run on a little-endian machine, along with explanatory calculation
  4. a5_1be.txt containing the output of your modified a5_1 program when run on a big-endian machine, along with explanatory calculation
  5. a5_2.c containing your modified version of the provided a5_2.c, as described above
  6. a5_2.txt containing your program's output and answers to the questions, as described above
  7. a5_3.c containing your modified version of the provided a5_3.c, as described above

File Format Requirements

Refer to the section of the same name in the second assignment for the file format requirements. All files you handin MUST be plain ASCII text, and all source code MUST compile in order to receive credit for it.


Last modified 2012-10-12 06:15