CSE 303: Autumn 2006, Assignment 4

Due: Monday 6 November 2006, 2:00pm

Updates

Assignment Overview

In this assignment, you will create a small simulation of the C heap. This assignment may be done individually or in pairs. If two people wish to work together, each should send an e-mail to Lincoln Ritter (lritter@cs) with the subject line "HW4 Pair" that contains both students' names and usernames. We suggest you choose a partner early and choose carefully. Once you decide to start working in pairs, you cannot change your mind.

If working in pairs, you will both receive the same grade. Pairs need turn in only one solution, but put both names in readme.txt, as explained in the standard turn-in instructions. Working in pairs is helpful to split up coding work. However, we suggest that paris of students work together to understand the concepts, because everyone will be responsible for understanding the concepts on exams.

Description

Your program must take a single command-line argument which should be interpreted as the size of a heap (heapsize), and simulate the action of allocating and freeing sections of this heap. The heap will be indexed by byte starting at 0 and continuing to heapsize-1. If your program is not given a single argument, or if heapsize is 0, print an error message on stderr and exit with a non-zero exit code.

While running, your program will process lines of input (separated by a newline character: '\n') received on stdin. You are guaranteed to receive no more than 126 characters between each newline character. Each line will be interpreted in one of four ways. If the first characters of a line match one of three commands (free, quit, or print) it will trigger an action described below. Otherwise, you should consider it to be an arbitrary string and copy it to your simulated heap. The following is a detailed description of each of these four cases.

You are fee to implement this assignment in any way you wish, as long as it has the correct behavior and produces correct output as described above. However, you are required to put structurally separate pieces of code in different files. For example, if you implement a linked list, you should put code for this linked list in a source file by itself. Also, your main function should appear in a source file by itself. If you do not use the file names and organization described below, you should describe the contents of each file in your program in readme.txt. You are also free to borrow code from any (legal) source other than students currently taking this course. If you borrow source code, describe the code and where it came from in readme.txt.

For testing, try running your program with a heap size of 50, entering the following sequence of commands, and directing your output to a file. If you diff your file with this one, they should be exactly the same.

a
b
c
free 2
free 0
dd
print
quit

Advice

Though you are free to implement your solution in any way you wish, this section describes how the sample solution was made. If you are unsure how to proceed, we suggest that you structure your program as we did.

The sample solution to this problem is spread across five files:

Programs like these can be very hard to debug! You might consider adding debugging code that prints lots of extra information. Be sure to remove this from the final version or make it so that the code is not compiled unless a particular flag is defined (as shown in class).

Note: You can get a Ph.D. in computer science by writing a good heap allocator. It is a very hard problem! Don't be surprised or disappointed if your implementation is inefficient.

Extra Credit

Remember the course policy on extra credit. You may do either or both of the following problems.

Assessment

Your solutions should be

Turn-in instructions

Use the standard turn-in instructions described here. If you use the structure we described, your directory should contain the files allocreclist.c, allocreclist.h, heap.c, heap.h, and heaptest.c. If you do not use the structure we described, you should include a description of the files in readme.txt. If you do any extra credit problems, you should also turn in readme.txt.