CS 61C (Fall 2007) |
Lab Assignment 3 |
These exercises are intended to give you practice implementing a dynamically resizeable structure (useful for handling the create command in project 1). and to familiarize you with the K&R storage allocation code.
Section 8.7 in K&R.
Find a partner, someone different from the person(s) you worked with in previous weeks. Introduce yourself to him or her, and tell each other your favorite book.
Copy the directory ~cs61c/files/lab/3/ to your home directory.
The file vector.c contains a framework for implementing a variable-length array (an ArrayList in Java). This exercise is designed to help familiarize you with C structs and memory management.
Fill in the missing code in vector.c and test it with the main program in test.c by typing
make test
If the test breaks, use gdb on the created executable (vector.test) or printf statements to find your bugs.
Comments in the code describe how the functions should work. Look at
the functions we've filled in to see how the data structures should be
used.
Note in particular that a request in vector_get for the value of a vector element
not previously set by vector_set should always return 0.
This implies that you'll need to do some extra work in vector_set
after a call to malloc. Consider using the memxxx()
functions in string.h
rather
than writing your own for
loops, but be careful that you calulate memory sizes in char
s,
byte
s, or int
s as appropriate.
When you resize the vector, keep two things in mind:
For checkoff, show your TA the output of vector.test along with the changes you made to the code. Also explain why the statement
free(v->size);
isn't needed in vector_delete ( ).
This exercise involves experimenting with the K&R storage allocation code. The code, augmented by a main function that repeatedly requests allocation and free requests from the user, is in the file stgmgmt.c. The format of user input is
a numberOfBytes f addressOfAllocReturn
The argument to the f command is a hexadecimal address previously returned by an a command. (This information is printed in the program's response to the allocation request.)
Answer the following questions about this code, and show your answers to your t.a. for checkoff. To find the answers, either add printf statements to the program or use gdb.