C-Lab 2 - The Arraylist

CS3410 Spring 2014

Due in the lab sessions of the week of Mar. 18.

Please submit required documents to CMS

To receive participation credit, you must sign in!

You must work ALONE for this and all other labs and homeworks. Failure to adhere to this rule will result in an Academic Integrity Violation. You will only be able to work in groups for projects. Updates to this assignment will be posted on the course web page.

We recommend that you work on CSUG machines for this lab.
For Linux and Mac users, simply type: 'ssh netid@csugXX.csuglab.cornell.edu' into the terminal. (Replace XX with a number between 01-14 and netid with your netID. Do not include quotes.)
For Windows users, download an SSH client.
The recommended SSH client is PuTTY. To connect to CSUG machines using PuTTY:
1.) Open PuTTY
2.) Under Host Name enter: 'netid@csugXX.csuglab.cornell.edu' (Replace XX with a number between 01-14 and netid with your netID. Do not include quotes.)
3.) Press Open
4.) A window labeled 'PuTTY Security Alert' should pop-up which provides a warning about the host key. Click Yes.
5.) You are connected.


In this lab we will implement 3 functions in the file arraylist.c for an arraylist of ints: arraylist_add, arraylist_insert, and arraylist_free.

References. Searching the Internet will generally find an answer to nearly any conceivable question about the C programming language. However, information on the Web is not always accurate or complete. We recommend reference books over web sites. In particular, C: A Reference Manual (5th Edition) by Samuel P. Harbison and Guy L. Steele Jr. is a required textbook for this class and should be your first source for reliable information on C.

The Arraylist

Download the file arraylist.c.

typedef struct arraylist {
    // update if necessary
    int *buffer;     // pointer to allocated memory
    int buffer_size; // the max number of integers the buffer can hold
    int length;      // the number of integers stored in the list
} arraylist;

A struct is an aggregate type that has accessible fields called members (see the slides for more information).

You will be implementing three functions in the file arraylist.c.

When you are done implementing the functions, compile your program using gcc and run it.

  $ gcc -g -Wall -std=c99 -o arraylist arraylist.c
  $ ./arraylist

If you have implemented everything correctly, the output should be:

    [0, 1, 2]
    Insert position 0: [100, 0, 1, 2]
    Insert position 1: [0, 100, 1, 2]
    Insert position 2: [0, 1, 100, 2]
    Insert position 3: [0, 1, 2, 100]
    Clean: [0, 1, 2]

You should use valgrind to check your code for memory leaks.

  $ valgrind -q --leak-check=full ./arraylist

What to submit

You have two lab sessions to work on this lab: the weeks of Mar. 4th and Mar. 18th. Submit your arraylist.c file by the end of the lab sessions in the week of Mar. 18.