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

  1. Compile buggy_list.c with debugging symbols enabled (-g).
  2. Run the executable. You will likely encounter a Segmentation Fault or an infinite loop.
  3. Use GDB to trace the program execution.
  4. Identify and fix the uninitialized pointer bug inside the insert_tail function.

Task 1.2: The Memory Leak

  1. Once the program runs without crashing, run it through Valgrind using the following command: valgrind --leak-check=full ./your_executable
  2. Note the “definitely lost” memory reported by Valgrind.
  3. Implement the free_list(Node* head) function in the source code to iterate through the list and free all dynamically allocated memory.
  4. Call this function at the end of main().
  5. 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:

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:

  1. Opens data.txt in read mode ("r").
  2. Reads the file line by line (using fgets or getline).
  3. Counts the total number of words in the file. (Assume words are separated by spaces or newline characters).
  4. Prints the final word count to the standard output.
  5. 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:


📎 Attached Resources