Skip to content

Leak Free

You'll build a real program that allocates and frees everything by hand, then run it under a checker until the report shows zero bytes lost.

About

In the Common badges you touched memory; here you own it. You'll write a word frequency counter with a dynamic array and a hash table you built yourself, so every byte comes from malloc and goes back through free, and then a tool checks your work. Valgrind on Linux and the address sanitizer anywhere will catch every leak, every double free and every write past the end, and a clean report is the whole point of this badge. Most people's first run shows a leak in an error path they never tested, and that's the lesson.

At a glance

Per requirement
25 XP
Completion bonus
150 XP
Requirements
6
Takes
One weekend
Skill level
Intermediate (months of practice)
Cost
Free
Supplies
You need some
Where
At home
Other people
On your own
Locked for now
Earn two more Common badges in Systems Programming to open this one. Log in to see how far along you are.

Requirements

  1. 1
    Build a dynamic array that grows with realloc, with push, get and free, and push a million integers. Start at a capacity of 4 and double when full. Check every malloc and realloc for NULL. Watch the memory in top or Activity Monitor while a million go in, then free it all. Take a screenshot of the test and the memory use staying sensible. Photo
  2. 2
    Write a hash table with string keys that owns a copy of each key and frees them all. Use separate chaining with a linked list in each bucket, and a simple hash like djb2. The table must copy each key on insert and free it on delete and on destroy, because the caller's string won't outlive the table. Take a screenshot of the table code and a test that inserts, looks up and deletes. Photo
  3. 3
    Count the words in a book-length text file with your table and print the top twenty. Project Gutenberg has free plain text books. Read it line by line, split on whitespace, lowercase the words, and count. Then sort the top twenty by count. Take a screenshot of the top twenty for one book. Photo
  4. 4
    Run it under valgrind or the address sanitizer and fix every leak and error until the report is clean. Use valgrind --leak-check=full ./counter book.txt on Linux, or build with -fsanitize=address,undefined -g on any platform. Fix the cause of each, not the symptom. All heap blocks were freed, no leaks are possible, is the line you want. Take a screenshot of the clean report. Photo
  5. 5
    Make a double free and a use after free on purpose and show the checker catching each. Free a key twice, then read from a bucket after the table is destroyed. The sanitizer names the line that freed it and the line that used it, which is far more than C alone gives you. Take a screenshot of both reports. Photo
  6. 6
    Feed it an empty file, a missing file and a 200 MB file, and show it handles all three without leaking. Error paths leak most, because nobody tests them. Make the big file with dd or your own program. Run all three under the checker, and expect the big one to take a while, because valgrind runs your program twenty to fifty times slower than normal. Take a screenshot of the three runs with a clean report each. Photo