CS 241 Data Organization using C
Lab 8: Structs, malloc, and valgrind


October 25, 2018

The purpose of lab today is to get you familiar with how to debug malloc/free in valgrind.

1 Getting Started

We’re going to get started writing code to help with homework 7 today. If you’ve already written code for this, start there. Otherwise, create a new c file called scheduler.c.

2 Creating a struct

Create a struct to represent a person at the university and call it Person. People all have the following attributes (you’re free to name these in your underlying data structure whatever you want as long as your names are appropriately descriptive):

In tandem with this, make the following creation function:

struct Person* createPerson(char* firstname, char* lastname, int  
    idnum, int student);

This method will create a Person struct, allocate space for them on the heap, instantiate all the fields in the struct, and then return a pointer on the heap.

3 Using valgrind

Similarly to gdb, in order to debug your code with valgrind, you need to use the -g flag.

valgrind --tool=memcheck will check the memory. The HEAP SUMMARY shouldn’t show anything in use at exit.

There are many other options to use. Some demoed in lecture valgrind --tool=memcheck --leak-check=yes --show-reachable=yes --num-callers=20 --track-fds=yes. Others are listed in the manual: http://valgrind.org/docs/manual/mc-manual.html.

4 To do during lab

Points will be allocated as follows

1
Correct Person struct. Code compiles cleanly.
1
Correct creation function for the Person struct. Code is tested in the main method and commented.
1
Code does not leak memory. Test your code with Valgrind to ensure this.

5 Turning in your program

It’s ok not to finish this assignment – get through as much as you can.

Zip up your scheduler.c file into a zip file entitled yourCSUsername.zip. Then drop the zip file into the dropbox under the lab-08 folder..