Crafting Interpreters · Week 10

Crafting Interpreters 19: Strings: reading guide

Read the chapterVerified by tests

Heap-allocated objects in clox, starting with strings: an Obj header, string objects, concatenation, and a linked list of all objects so they can be freed at exit.

Time About 5 hours this week

Why it matters

This is where manual memory management starts in earnest. The object header plus linked list is the foundation for the garbage collector later.

How to approach it

Run every test under AddressSanitizer and LeakSanitizer. Understand the struct inheritance trick (an Obj as the first field) before moving on.

Challenges

3 challenges at the end of the chapter.

Challenge 1 (flexible array members to store characters inline) is a classic C optimization worth doing. Challenge 2 (strings that do not own their characters) is a good follow-up.

Traps

  • Forgetting the null terminator or the +1 in allocations.
  • Leaking the intermediate string in concatenation.
  • Casting between Obj* and ObjString* incorrectly.

Verified by the book's chap19_strings tests when you push your repo.

Links

Back to week 10