Mini Lab: Structs and Dynamic Memory
This is a mini lab intended to be completed in class. You have two goals:
- Use what you’ve learned about structs to update one of your past labs (you can choose which!).
- Use a struct to represent the program’s options
- Create an instance of the struct that begins with the program’s default settings
- Adjust the struct as you process command line options
- Practice passing the struct to a function. If you already implemented your program logic outside of
main
, e.g., maybe in thecat
lab you have acat
function that handles most of the program logic, pass the struct to the function so that it has access to the settings.- If you didn’t design your past lab that way, then now is a great time to refactor it so you can implement this!
- Create a copy of the lab you just added structs to and refactor it so that all variables are dynamically allocated on the heap with
malloc
instead of on the stack.- Once you’re finished and your program works as intended, run it under
valgrind
to ensure there are no memory leaks.valgrind --track-fds=yes --track-origins=yes --leak-check=full program_name
- Once you’re finished and your program works as intended, run it under
Grading
Demonstrate both goals above for full credit. If you only complete the first goal, then you will earn 50% credit.