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. 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.
Download the file arraylist.c.
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.
arraylist_addto 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).
arraylist_freesimply has to free all the resources used by an arraylist.
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.
Start by running GDB with
$ gdb ./arraylistGDB 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:
break arraylist_printto place a breakpoint at the beginning of the arraylist_print() function. Then type
runto start your program. Soon you will find that execution comes grinding to a halt. Type
backtraceto see the call stack. You can now use
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.
You should use valgrind to check your code for memory leaks.
$ valgrind -q --leak-check=full ./arraylist