Step 4: keep the mistakes
The last step lists the wrong answers at the end. That is the first time the program has to keep
text: each mistake is known when it's made, and printed only after the last question. The buffer
can't hold it, since the next answer overwrites it. A string made with new would never be freed.
The program needs memory that lives exactly as long as the list of mistakes.
That is what an arena is for. An arena hands out memory from blocks it allocates as needed, and frees it all at once. One arena per lifetime is the pattern: here, one for the data that lasts until the end of the program.
// Lives until the end: the mistakes are kept here, the list and its strings.
Arena keep = Arena.Create(4096);
RefList<string> mistakes = RefList<string>.Create(keep);
if (!mistakes.IsCreated)
{
Out.Line("Arena.Create failed");
return 1;
}
Arena.Create makes the arena, with a first block of 4 KB that grows if needed.
RefList<string>.Create(keep) makes an empty list in it. The list holds references, which the plain
List<T> can't: List<T> stores items by value and only takes types without references.
If the arena couldn't be created, nothing built on it can be either, so checking the list is enough.
Record a mistake
else
{
Out.Line($"no, it's {a + b}");
// Kept until the end, so the string is built in `keep`, not in
// the buffer the next answer overwrites.
mistakes.Add(Str.Format(keep, $" {a} + {b} = {a + b}, not {value}"));
}
Str.Format(keep, $"...") is the one place in the program that builds a string. It formats the line,
like Out.Line would, into a new string allocated in keep. The string lives as long as the arena,
and the list holds a reference to it.
Both must live in an arena that outlives their use. The list could live in keep while its strings
lived in a scratch arena that was reset: the list would then point at freed memory. Keeping the list
and its strings in the same arena avoids that.
Report
Out.Line($"{right} of {asked} right");
if (mistakes.Count > 0)
{
Out.Line("Mistakes:");
foreach (string mistake in mistakes)
Out.Line(mistake);
}
foreach works on RefList, as it does on Dict, StrDict and RefDict; a List is looped over
with Count and Get. When Main returns, the program ends
and the operating system takes back its memory, arena included. A program that kept running would
free it once it no longer needs the mistakes, with unsafe { keep.Destroy(); }, or call Reset()
the same way to reuse the memory for the next round. Both are unsafe: they leave every pointer into
the arena dangling, so the call needs a block and a comment saying why nothing uses the mistakes
afterwards (the memory safety rules).
Run it
./build.sh Quiz
./run.sh Quiz < tests/input/Quiz.txt
The output ends with the list:
3 of 5 right
Mistakes:
5 + 0 = 5, not 7
Where next
- Memory without a garbage collector explains arenas and lifetimes, including how to free temporary work inside a longer-lived arena.
- Keep a string and choose a collection cover the other ways to hold on to text.
- The reference lists everything in
In,Out,ArenaandRefList.