When decoder_feed produces a reading, how does the caller receive it? There are two broad designs. The decoder can return a view: a pointer into its own frame buffer, with no copying. Or it can publish a copy: write a complete Reading value into an object the caller owns. Views are faster for large payloads, but they come with a lifetime question that every caller must answer correctly. The next feed overwrites the buffer, reset clears it, destroy frees it, and any of those silently changes or invalidates what the view points to.
For a four-byte payload, the copy costs nothing worth measuring, and it removes the lifetime question entirely. So the project's contract is:
decoder_feedwrites*outonly when it returnsFRAME_READY.- Partial input, a bad header, a checksum mismatch, a domain failure and sticky error all leave
*outexactly as the caller left it. - The decoder never returns a pointer into its internal buffer, so a completed reading stays valid after reset or destroy.
Predict and run
Read the program before pressing Run. Predict the output, then change valid to 1 and predict again.
#include <stdio.h>
typedef struct { unsigned sensor; unsigned value; } Reading;
int main(void) {
Reading out = {77,88};
Reading candidate = {1,25};
int valid = 0;
if (valid) { out = candidate; }
printf("%u %u\n", out.sensor,out.value);
return 0;
}Check your prediction
77 88The candidate is built in a local variable. Only one statement, guarded by the validity test, copies it into out, and struct assignment copies both fields as a unit. With valid set to 0 the sentinel survives; with 1 the output becomes 1 25. There is no state in between, which is the property you want callers to be able to rely on.
Views and their expiry
To see why the project avoids views, imagine an API that returns a pointer into the decoder's buffer:
const unsigned char *decoder_view(const Decoder *decoder); /* hypothetical */Its documentation would have to say the view is valid only until the next call to feed, reset or destroy on that decoder. A caller that stores the pointer in a list of "recent readings" breaks that rule without any warning from the compiler: after the next feed, the stored pointer shows the new frame's bytes; after destroy, reading it is undefined behavior. Some libraries accept that cost for large payloads and document it carefully. For a two-field reading, copying is simpler and leaves nothing to get wrong. If a future frame type carried kilobytes, a view with a precise expiry rule might become worth it, and that decision would belong in the header comment.
Overlap is excluded by contract
The header also requires that out does not overlap the decoder or the input bytes. That is what makes the final assignment safe to reason about: writing the caller's Reading cannot change a byte the decoder is still reading. Breaking a no-overlap rule is a caller bug the library does not try to detect.
Torn output, in one sentence
You already know the failure this prevents from A Checksum Is Evidence, Not Authentication and Day 3's checkpoint: a decoder that writes out->sensor and then rejects the value leaves the caller holding a reading no device ever sent, while reporting failure. The candidate-then-assign shape above is the whole cure, so the rest of this lesson spends its time on the design the project rejects: handing out a view instead of a copy.
Practice
Your turn
The project copies, but you should be able to build the other design when a payload is large enough to justify it, and make its expiry rule checkable instead of merely documented. The starter has a cut-down decoder Mini whose generation counter increases every time a completed frame or a reset overwrites its buffer, and a View type: a borrowed pointer, a size and the generation it was taken in. Write:
View decoder_view(const Mini *d), a view of the 4 payload octets of the completed frame (offset 5), with no copying;int view_valid(const Mini *d, View v), which returns 1 only while nothing has overwritten the buffer since the view was taken.
The starter takes a view, completes a second frame, takes a fresh view and resets. It must print:
view: valid, sensor 1 value 25
after next frame: expired
stale bytes now say sensor 2; copy still 1 25
fresh view: valid, sensor 2 value 40
after reset: expired#include <stdio.h>
#include <string.h>
typedef struct { unsigned sensor; unsigned value; } Reading;
typedef struct { unsigned char frame[10]; size_t used; unsigned long generation; } Mini;
typedef struct { const unsigned char *bytes; size_t size; unsigned long generation; } View;
/* Stand-ins for the decoder: a completed frame or a reset overwrites the buffer. */
static void complete_frame(Mini *d, const unsigned char frame[10]) {
memcpy(d->frame, frame, 10);
d->used = 10;
++d->generation;
}
static void reset(Mini *d) {
memset(d->frame, 0, sizeof d->frame);
d->used = 0;
++d->generation;
}
static Reading reading_from(const unsigned char *payload) {
Reading r = {((unsigned)payload[0] << 8) | payload[1], ((unsigned)payload[2] << 8) | payload[3]};
return r;
}
/* Write decoder_view and view_valid here, each with its contract comment. */
static void show(const char *label, const Mini *d, View v) {
if (view_valid(d, v)) {
Reading r = reading_from(v.bytes);
printf("%s: valid, sensor %u value %u\n", label, r.sensor, r.value);
} else {
printf("%s: expired\n", label);
}
}
int main(void) {
const unsigned char first[10] = {0x54, 1, 1, 0, 4, 0, 1, 0, 25, 0x74};
const unsigned char second[10] = {0x54, 1, 1, 0, 4, 0, 2, 0, 40, 0x84};
Mini d = {{0}, 0, 0};
complete_frame(&d, first);
View v = decoder_view(&d);
Reading copy = reading_from(v.bytes);
show("view", &d, v);
complete_frame(&d, second);
show("after next frame", &d, v);
printf("stale bytes now say sensor %u; copy still %u %u\n",
reading_from(v.bytes).sensor, copy.sensor, copy.value);
View fresh = decoder_view(&d);
show("fresh view", &d, fresh);
reset(&d);
show("after reset", &d, fresh);
return 0;
}Hint 1: the idea
A raw pointer cannot tell you whether the bytes it points at are still the ones you meant. Stamp the view with something the decoder changes every time it overwrites the buffer, and compare that stamp later.
Hint 2: a first step
Write decoder_view without a generation, and make view_valid return 1 for now. Run it: the second line says valid, sensor 2 value 40. The old view silently shows a different reading, the bug a documented expiry rule cannot stop. Now make that line say expired.
Show one solution
#include <stdio.h>
#include <string.h>
typedef struct { unsigned sensor; unsigned value; } Reading;
typedef struct { unsigned char frame[10]; size_t used; unsigned long generation; } Mini;
typedef struct { const unsigned char *bytes; size_t size; unsigned long generation; } View;
/* Stand-ins for the decoder: a completed frame or a reset overwrites the buffer. */
static void complete_frame(Mini *d, const unsigned char frame[10]) {
memcpy(d->frame, frame, 10);
d->used = 10;
++d->generation;
}
static void reset(Mini *d) {
memset(d->frame, 0, sizeof d->frame);
d->used = 0;
++d->generation;
}
static Reading reading_from(const unsigned char *payload) {
Reading r = {((unsigned)payload[0] << 8) | payload[1], ((unsigned)payload[2] << 8) | payload[3]};
return r;
}
/* Borrows the 4 payload octets of the completed frame. The view expires at
the decoder's next completed frame or reset; check it with view_valid. */
View decoder_view(const Mini *d) {
View v = {&d->frame[5], 4, d->generation};
return v;
}
/* 1 while nothing has overwritten the buffer since the view was taken. */
int view_valid(const Mini *d, View v) {
return v.bytes == &d->frame[5] && v.generation == d->generation;
}
static void show(const char *label, const Mini *d, View v) {
if (view_valid(d, v)) {
Reading r = reading_from(v.bytes);
printf("%s: valid, sensor %u value %u\n", label, r.sensor, r.value);
} else {
printf("%s: expired\n", label);
}
}
int main(void) {
const unsigned char first[10] = {0x54, 1, 1, 0, 4, 0, 1, 0, 25, 0x74};
const unsigned char second[10] = {0x54, 1, 1, 0, 4, 0, 2, 0, 40, 0x84};
Mini d = {{0}, 0, 0};
complete_frame(&d, first);
View v = decoder_view(&d);
Reading copy = reading_from(v.bytes);
show("view", &d, v);
complete_frame(&d, second);
show("after next frame", &d, v);
printf("stale bytes now say sensor %u; copy still %u %u\n",
reading_from(v.bytes).sensor, copy.sensor, copy.value);
View fresh = decoder_view(&d);
show("fresh view", &d, fresh);
reset(&d);
show("after reset", &d, fresh);
return 0;
}view: valid, sensor 1 value 25
after next frame: expired
stale bytes now say sensor 2; copy still 1 25
fresh view: valid, sensor 2 value 40
after reset: expiredThe view costs nothing to make, but every use has to ask view_valid first, and the third line shows why: the stale pointer still points at real memory, which now holds the next frame, so nothing crashes and nothing warns. The copy taken from the first view is unaffected by anything the decoder does later, which is the guarantee the project buys by publishing a Reading value. A generation stamp catches misuse only for callers that check it, and only while the decoder itself is alive: after destroy, even calling view_valid would read freed memory. For a four-octet payload the copy wins; for kilobytes, this checkable view is the design you would document in the header. The checkpoint next decides what happens to the decoder itself after a rejection.
Completion is saved in this browser only. It does not measure mastery.