CS61C Lab 3

Malloc

Background

Goals

This lab will give you practice working with the malloc function.

Reading

  • K&R: 6.1-6.5

Exercises

Partners

Working with a partner is highly recommended. If you work with a partner, be sure both partners understand all aspects of your solution.

Setup

Copy the contents of ~cs61c/labs/03 to a suitable location in your home directory.

$ mkdir ~/lab
$ cp -r ~cs61c/labs/03/ ~/lab

Exercise 0: Updating your Gecos Information

Begin by typing the following command into your UNIX prompt, replacing "<login>" with your cs61c login.

  % finger | grep <login>

For example, if your login is cs61c-ab, then you would have typed the following:

  % finger | grep cs61c-ab

The result of typing the command should give you an output as follows:

  cs61c-ab Albert Bentley        pts/6            Tue 13:11  nova               

If the 2nd column says "Class Account", you will need to change your Gecos information, the name associated with your account. To do that, please ssh into update (type "ssh update" into the UNIX prompt--it will ask you to enter your password). Once you have logged into update, select "Change your Gecos" and enter a name that is not "Class Account". You don't need to type your real name, but should be something that will give your account an unique name.

Show that your Gecos information is not "Class Account" 

Exercise 1: part1.c

Open part1.c. Describe (either as comments in this file or a separate document) the effect of each of the statements and answer the following questions:

  1. What areas of memory (heap or stack) are affected by the statement?
  2. Does any memory get allocated or freed?
  3. If so, where is this memory?
  4. Does the statement result in a memory leak?

Feel free to use gdb or printf statements to help in your investigation. Additionally, both of the malloc statements work, but one of them is in bad form. Which is "bad," why is it "bad," and how would you change it?

Checkoff

Show your answer to your TA. 

Exercise 2: vector.c

In vector.c, we provide you with a framework for implementing a variable-length array (in Java and C++, these variable-length arrays are known as the Vector class). This exercise is designed to help familiarize you with C structs and memory management in C.

Fill in the missing code (you only need to modify vector.c), and test it by running 'make test'. If the test breaks, try using gdb (or ddd on the cory server) on the created executable (vector.test) or printf statements to find your bugs. If you have extra time, you can try adding more test cases to the code in test.c and explore how we've split up this one program into two different .c files.

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.

For consistency, it is assumed that all entries in the vector are 0 unless set by the user. Keep this in mind as malloc() does not zero-out the memory it allocates.

Checkoff

Run vector.test and show it to your TA 
Show your TA your modifications in vector.c 
Explain why free(v->size); statement is not needed in vector_delete()