C-Lab 3 - The Arraylist

CS3410 Spring 2015

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

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.

Overview

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. You will also gain experience debugging code using GDB.

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.

Step 1: Implement the arraylist_add function

The elements of an arraylist are stored consecutively in a dynamically allocated array. This function takes a pointer to an arraylist and a value, and appends that value to the end of the arraylist. It should also increment the length of the arraylist to include the new element.

If the arraylist is currently at capacity when arraylist_add is called, you should create a new dynamic array that is twice the size of the old one and copy over all the elements currently in the arraylist. You may find that the standard library function realloc is useful for doing this.

Step 2: Implement the arraylist_insert function

This function should take advantage of your arraylist_add to insert an element at an arbitrary location in your arraylist. You do not have to worry about attempting to insert to locations beyond the end of the arraylist (doing so it undefined behavior).

Step 3: Implement the arraylist_free function

The arraylist_free simply has to free all the resources used by an arraylist.

Step 4: Test

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

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

The program is meant to output:

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

Unfortunately, you will discover that the program contains a bug that causes it to print slightly different output. Advance to the next step to discover how to use GDB to find it.

Step 5: Debugging

Start by running GDB with

    $ gdb ./arraylist
GDB will start by printing some license information and loading your program. You will then be presented by a prompt. You can find detailed documentation and tutorials online. Some of the most useful commands are: After starting GDB, the first thing you'll want to do is place a breakpoint. Type break arraylist_print to place a breakpoint at the beginning of the arraylist_print() function. Then type run to start your program. Soon you will find that execution comes grinding to a halt. Type backtrace to see the call stack. You can now use print to view the values of variables. Pay special attention to the contents of the arraylist. You can continue execution by typing continue. Once you have a theory of what might be going wrong, you'll want to run your program again. This time before doing so, set a different breakpoint in the code where you think the issue might be. Again inspect the program state to see if you've tracked down the problem. If not, try somewhere else in the program and repeat.

Step 6: Valgrind

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

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

Step 7: Have a TA check over your final program

Congratulations, you've completed C lab 3 and now have a even deeper understanding of arrays, pointers, and memory management in C!