Addressing your Memory Errors
The relevant files for this part of the lab are found within the valgrind
directory in the lab7-starter
repository you cloned earlier.
In this directory, you will find a Makefile
, jstr.c
and arraylist.c
which contain the implementations of the String
and List
structs you have seen in lecture. In addition to these files, there are 6 additional C files labeled q1-q6
.
Each of these q
files implements function(s) on the String
and List
structs but with one or more memory related errors in them. Your task is to use valgrind
to determine these errors and fix them. The files are numbered in increasing order of difficulty, so it is recommended to start with q1_expand_list.c
and work your way up.
TASKS:
-
Open
jstr.c
andarraylist.c
and familiarize yourself with the implementations of theString
andList
structs. -
Run the
make
command to compile all the buggy C files. You should now have 6 executables in your directory. (Check this by usingls
). -
For each program (starting with
q1
), run it both with and withoutvalgrind
to see what happens. Use the program output (whether it crashed or not) andvalgrind
to determine where the error may be and edit the C file to fix it. Remember to use--leak-check=full
to get more detailed information about memory leaks.NOTE: The names of the files as well as the function names within the files are hints for where the error may be.
NOTE: All the changes should be in the
buggy_
functions found in each file. Themain
function should not need to be modified. -
Once you have fixed the error(s) in a file, run the program with
valgrind
again to ensure that there are no memory leaks or other memory-related errors. Yourvalgrind
output should look like: All heap memory should be freed and there should be no errors reported in theERROR SUMMARY
. -
When you determine the bug in each of the files, describe the error you found and how you fixed it in your notes. Provide a screenshot of the
valgrind
output for the fixed program.