Assignment 0: Prequisite Self-Assessment
Weight: 0% (Formative Assessment)
Environment Requirements: Linux CLI, GCC, Make, GDB, Valgrind
Overview
This assignment is designed to help you gauge your readiness for the course. It tests your proficiency in C programming, memory management, low-level debugging, and the Linux toolchain.
If you find these tasks significantly challenging, you must review the prerequisite guide for this course and bridge your knowledge gaps before the first graded lab.
Part 1: Debugging and Memory Management (C Programming)
You have been provided with a starter file named buggy_list.c. It contains a basic implementation of a singly linked list. However, it contains two major issues: a critical memory initialization bug and a severe memory leak.
Task 1.1: The Segmentation Fault
- Compile
buggy_list.cwith debugging symbols enabled (-g). - Run the executable. You will likely encounter a Segmentation Fault or an infinite loop.
- Use GDB to trace the program execution.
- Identify and fix the uninitialized pointer bug inside the
insert_tailfunction.
Task 1.2: The Memory Leak
- Once the program runs without crashing, run it through Valgrind using the following command:
valgrind --leak-check=full ./your_executable - Note the “definitely lost” memory reported by Valgrind.
- Implement the
free_list(Node* head)function in the source code to iterate through the list and free all dynamically allocated memory. - Call this function at the end of
main(). - Re-run Valgrind to verify that the heap usage summary reports:
All heap blocks were freed -- no leaks are possible.
Part 2: Build Automation (Makefiles)
Writing compile commands manually becomes tedious quickly. You must know how to automate your builds.
Task 2.1: Write a Makefile
Create a file named Makefile in the same directory as your source code. It must meet the following specifications:
- Define a compiler variable (
CC=gcc). - Define compiler flags (
CFLAGS=-Wall -Wextra -g). - Create a target named
allthat builds an executable namedlist_testfrombuggy_list.c. - Create a target named
cleanthat removes the executable (rm -f list_test).
Verification: Typing make in your terminal should compile the code, and typing make clean should delete the binary.
Part 3: Systems Programming Basics (File I/O)
Operating systems constantly interact with files. You must be comfortable reading from files and parsing strings in C.
Task 3.1: Text Processing
You have been provided with a text file named data.txt. Write a new C program named word_counter.c that does the following:
- Opens
data.txtin read mode ("r"). - Reads the file line by line (using
fgetsorgetline). - Counts the total number of words in the file. (Assume words are separated by spaces or newline characters).
- Prints the final word count to the standard output.
- Safely closes the file descriptor using
fclose.
Constraints: Do not hardcode the filename; pass it as a command-line argument (e.g., ./word_counter data.txt). Ensure your program gracefully handles the case where the file does not exist.
Submission Checklist
Before considering yourself ready for the course, ensure you have successfully completed:
- A fixed
buggy_list.cthat passes Valgrind with 0 leaks. - A functioning
Makefilewithallandcleantargets. - A functioning
word_counter.cthat accurately readsdata.txtand handles missing file errors.
📎 Attached Resources
- Download: buggy_list.c (text)
- Download: data.txt (text)