Each piece of the analyzer is something you have already built: a bounded line reader (Day 4), a record parser that commits only on success (Day 4), a growing collection with a failure-safe append (Day 3), a sorted, grouped report (Day 5), and one cleanup path (Day 6). This lesson follows a line of input through all of them, so that when you open the source you can say why each line is there. Read with three questions in mind: who owns this memory, what happens if this call fails, and how long is this pointer valid?
Predict and run
Read the program before pressing Run and write down its output.
#include <stdio.h>
int main(void) {
int minutes[] = {25, 0, 181, 40, 180};
size_t accepted = 0, rejected = 0;
for (size_t i = 0; i < sizeof minutes / sizeof minutes[0]; ++i) {
if (minutes[i] >= 1 && minutes[i] <= 180) {
++accepted;
} else {
++rejected;
}
}
printf("Accepted: %zu, rejected: %zu\n", accepted, rejected);
printf("Processed: %zu\n", accepted + rejected);
return 0;
}Check your prediction
Accepted: 3, rejected: 2
Processed: 50 and 181 fall outside 1 to 180, and 180 itself is inside, so three values are accepted. This is the accounting rule for the whole program: every input line ends up in exactly one of the two counts. If accepted plus rejected ever differs from the number of lines, some path counted a line twice or not at all.
The path of one line
main.c owns a 96-byte line buffer, a rejected counter and one Analyzer. For each line:
fgetsfills the buffer. If there is no newline and the stream is not at its end, the line is too long: drain it, count one rejection,continue.record_parse(line, &record)validates into a local candidate and copies it torecordonly on success. OnANALYZER_INVALID, count a rejection andcontinue.analyzer_append(&analyzer, &record)copies the record into owned storage, growing if needed.ANALYZER_FULLandANALYZER_INVALIDare rejections.ANALYZER_NOMEMis fatal.
After the loop, main checks ferror(stdin), calls analyzer_report, prints the summary from analyzer.length and analyzer_total, and checks ferror(stdout). record lives on the stack and is reused on every iteration; that is safe because analyzer_append copies it by value.
Ownership, from init to destroy
analyzer_init sets data to NULL and both counts to 0, and stores the allocator. From then on, analyzer.data is owned by the analyzer and changed only by its functions. Growth replaces it through a temporary after resize succeeds. The report allocates a second, temporary block, copies the records into it, sorts and prints it, and releases it before returning: that block's whole lifetime is inside one function call. At cleanup:, analyzer_destroy releases data and resets the fields. So the program holds at most two blocks at once, and both are released on every path.
The append detail that is easy to miss
Read this part of analyzer_append:
/* A caller may pass &data[i]. Copy it before realloc can invalidate it. */
Record candidate = *record;
if (analyzer->length == analyzer->capacity) {
/* ... resize, which may move data ... */
}
analyzer->data[analyzer->length] = candidate;main never passes a pointer into the collection, so ordinary runs would work without the copy. But the header promises the function "copies record by value", and a caller duplicating an existing record would write analyzer_append(&a, &a.data[0]). If growth moved the block, reading *record afterward would be a use-after-free. Copying first makes the promise true for every caller, and test_analyzer.c checks exactly this call across a growth.
Rejected versus fatal
A rejected record is the input's problem: the program can report it and keep going, and the result is still a correct report of the valid lines. Out of memory is the program's problem: it can no longer store what the user gave it, so any report would silently leave records out. That is why main treats ANALYZER_NOMEM differently from every other status. The library only reports the status; the policy lives in main.
Practice
Your turn
Write Action decide(Status status) that maps STATUS_OK to ACTION_COUNT_ACCEPTED, STATUS_INVALID and STATUS_FULL to ACTION_COUNT_REJECTED, and STATUS_NOMEM to ACTION_STOP. The starter simulates a run over a list of statuses and stops at the first ACTION_STOP. Expected output: accepted 2, rejected 2, stopped: yes.
#include <stdio.h>
#include <stddef.h>
typedef enum { STATUS_OK, STATUS_INVALID, STATUS_FULL, STATUS_NOMEM } Status;
typedef enum { ACTION_COUNT_ACCEPTED, ACTION_COUNT_REJECTED, ACTION_STOP } Action;
/* Write decide here. */
int main(void) {
Status results[6] = {STATUS_OK, STATUS_INVALID, STATUS_OK, STATUS_FULL, STATUS_NOMEM, STATUS_OK};
size_t accepted = 0, rejected = 0;
int stopped = 0;
for (size_t i = 0; i < 6 && !stopped; ++i) {
switch (decide(results[i])) {
case ACTION_COUNT_ACCEPTED: ++accepted; break;
case ACTION_COUNT_REJECTED: ++rejected; break;
case ACTION_STOP: stopped = 1; break;
}
}
printf("accepted %zu, rejected %zu, stopped: %s\n", accepted, rejected, stopped ? "yes" : "no");
return 0;
}Hint 1: the idea
The last STATUS_OK comes after the out-of-memory result, so it must never be counted. Stopping is a decision about the rest of the input, not about one line.
Hint 2: a first step
List the four statuses and the action each one maps to on paper first, then write decide as a switch with one case per status. Leave out default and ask why: what should the compiler say if a fifth status is added to the enum later?
Show one solution
#include <stdio.h>
#include <stddef.h>
typedef enum { STATUS_OK, STATUS_INVALID, STATUS_FULL, STATUS_NOMEM } Status;
typedef enum { ACTION_COUNT_ACCEPTED, ACTION_COUNT_REJECTED, ACTION_STOP } Action;
Action decide(Status status) {
switch (status) {
case STATUS_OK: return ACTION_COUNT_ACCEPTED;
case STATUS_INVALID:
case STATUS_FULL: return ACTION_COUNT_REJECTED;
case STATUS_NOMEM: return ACTION_STOP;
}
return ACTION_STOP;
}
int main(void) {
Status results[6] = {STATUS_OK, STATUS_INVALID, STATUS_OK, STATUS_FULL, STATUS_NOMEM, STATUS_OK};
size_t accepted = 0, rejected = 0;
int stopped = 0;
for (size_t i = 0; i < 6 && !stopped; ++i) {
switch (decide(results[i])) {
case ACTION_COUNT_ACCEPTED: ++accepted; break;
case ACTION_COUNT_REJECTED: ++rejected; break;
case ACTION_STOP: stopped = 1; break;
}
}
printf("accepted %zu, rejected %zu, stopped: %s\n", accepted, rejected, stopped ? "yes" : "no");
return 0;
}accepted 2, rejected 2, stopped: yesTwo empty cases in a row are deliberate sharing, not an accidental fall-through: no statement sits between them. An unknown value falls back to ACTION_STOP, the safe choice when the program cannot tell what happened. The real main does the same with goto cleanup. Now run the complete application.
Run the complete application
The input box is prefilled with the sample from the brief.
Check your prediction
coding: 1 sessions, 40 minutes
reading: 2 sessions, 35 minutes
Accepted: 3
Total: 75 minutes
Rejected: 2Then try these inputs and predict each report before running: an empty input box (No records and three zeros); reading 25, a bad line and coding 40 (both valid records, sorted by topic, one rejection); and an overlong line followed by reading 25. The long line must count as exactly one rejection, and the next line must still be read.
Completion is saved in this browser only. It does not measure mastery.