A program that reads telemetry from a file or a socket usually owns one receive buffer and reuses it: read 512 bytes, hand them to the decoder, read the next 512 bytes into the same array. If the decoder kept a pointer into that array between calls, the next read would silently overwrite the bytes it was still counting on. If the caller freed the buffer, the decoder would be holding a dangling pointer. Neither mistake shows up in a type signature.
So every pointer in a C API needs a lifetime rule, not just a type. The project uses three:
- Input is borrowed for the call only.
decoder_feedmay read the bytes while it runs and must not keep their address afterward. A partial frame is copied into decoder-owned storage before the call returns. - The decoder owns its internal buffer. That storage lives exactly as long as the handle.
- Completed output is copied to the caller. A
Readingis written into an object the caller owns, so it stays valid after reset or destroy.
Predict and run
Read the program before pressing Run. Write down its output and say which statement produces it.
#include <stdio.h>
#include <string.h>
int main(void) {
unsigned char source[3] = {1,0,2};
unsigned char copy[3];
memcpy(copy, source, sizeof source);
source[0] = 9;
printf("%u %u\n", (unsigned)copy[0], (unsigned)copy[1]);
return 0;
}Check your prediction
1 0After memcpy, copy has its own three bytes. Changing source[0] afterward cannot reach them. That independence is the whole point of copying: the copy's lifetime no longer depends on the source. Also notice the zero in the middle. It is an ordinary data byte, and memcpy copied it like any other.
Keep a pointer or keep the bytes
The next program contrasts two ways to remember input. Keeper stores the caller's pointer; Stash copies the bytes. The caller then reuses its buffer, exactly as a receive loop would. Nothing here is undefined, because the buffer is still alive, but only one of the two remembers what it was given.
#include <stdio.h>
#include <string.h>
typedef struct { const unsigned char *bytes; } Keeper;
typedef struct { unsigned char bytes[2]; } Stash;
int main(void) {
unsigned char receive[2] = {0x54, 0x01};
Keeper keeper = {receive};
Stash stash;
memcpy(stash.bytes, receive, sizeof stash.bytes);
receive[0] = 0xAA; /* the next read reuses the buffer */
receive[1] = 0xBB;
printf("Keeper sees: %02X %02X\n", (unsigned)keeper.bytes[0], (unsigned)keeper.bytes[1]);
printf("Stash kept: %02X %02X\n", (unsigned)stash.bytes[0], (unsigned)stash.bytes[1]);
return 0;
}Keeper sees: AA BB
Stash kept: 54 01Keeper now reports bytes that never belonged to the first message. If receive had been a local array in a function that already returned, reading through keeper.bytes would be undefined behavior instead of merely wrong. AddressSanitizer can catch the use of a freed heap buffer, and some uses of a dead stack frame, but only on the runs where the bad access actually happens. A lifetime rule in the header prevents the mistake on every run.
A pointer and a length, not a string
Wire data may contain zero bytes anywhere. A string function such as strlen stops at the first zero, so it cannot measure binary input:
#include <stdio.h>
#include <string.h>
int main(void) {
const char frame[5] = {0x54, 0x01, 0x01, 0x00, 0x04};
printf("strlen: %zu, real size: %zu\n", strlen(frame), sizeof frame);
return 0;
}strlen: 3, real size: 5Every function in the project therefore takes a pointer together with an explicit count: const unsigned char *bytes, size_t size. The const says the library will not modify the borrowed bytes. The count says exactly how many may be read. Reading bytes[size] is out of bounds even if memory happens to exist there.
Zero length, NULL and overlap
What should happen when a caller has nothing to send? The project allows bytes == NULL when size == 0, so a caller with an empty buffer does not have to invent a pointer. That permission must be stated, and the implementation must honor it carefully. In C17, passing NULL to memcpy is undefined behavior even when the count is zero, because the standard requires valid pointers for every call. A loop that runs zero times never dereferences its pointer, so it is safe, but a "harmless" memcpy(dest, NULL, 0) is not.
Overlap is the other clause. memcpy requires the source and destination not to overlap; memmove allows it at some cost. The decoder's contract says the decoder, the input and the output must be separate objects. That lets the implementation copy freely and lets you reason about each write: storing a Reading cannot change the input bytes still being read. Breaking a no-overlap rule is undefined behavior, and optimizers are allowed to assume it never happens.
Practice
Your turn
A Stash owns four bytes and a used count. Write size_t stash_take(Stash *stash, const unsigned char *bytes, size_t count) that copies as many bytes as still fit, returns how many it copied, and allows bytes == NULL when count is 0 without calling memcpy on it. In main, take {1,0,2}, overwrite the caller's buffer with {7,8} and take two bytes, then take NULL with count 0. Print:
Took 3 of 3
Took 1 of 2
Stash: 01 00 02 07
Empty call took 0#include <stdio.h>
#include <string.h>
typedef struct { unsigned char bytes[4]; size_t used; } Stash;
int main(void) {
Stash stash = {{0}, 0};
unsigned char receive[3] = {1, 0, 2};
/* Write stash_take above and call it here. */
(void)stash; (void)receive;
return 0;
}Hint 1: the idea
The stash has 4 - used free bytes, and used never exceeds 4, so that subtraction is safe. Copy the smaller of the free space and count.
Hint 2: a first step
Return 0 at the top when count is 0. That one guard both honors the empty-input contract and keeps NULL away from memcpy.
Show one solution
#include <stdio.h>
#include <string.h>
typedef struct { unsigned char bytes[4]; size_t used; } Stash;
size_t stash_take(Stash *stash, const unsigned char *bytes, size_t count) {
if (count == 0) { return 0; }
size_t space = sizeof stash->bytes - stash->used;
size_t take = count < space ? count : space;
memcpy(stash->bytes + stash->used, bytes, take);
stash->used += take;
return take;
}
int main(void) {
Stash stash = {{0}, 0};
unsigned char receive[3] = {1, 0, 2};
printf("Took %zu of 3\n", stash_take(&stash, receive, 3));
receive[0] = 7; receive[1] = 8;
printf("Took %zu of 2\n", stash_take(&stash, receive, 2));
printf("Stash: %02X %02X %02X %02X\n", (unsigned)stash.bytes[0], (unsigned)stash.bytes[1],
(unsigned)stash.bytes[2], (unsigned)stash.bytes[3]);
printf("Empty call took %zu\n", stash_take(&stash, NULL, 0));
return 0;
}Took 3 of 3
Took 1 of 2
Stash: 01 00 02 07
Empty call took 0The stash still shows 01 00 02 after the caller overwrote its buffer, because those bytes were copied during the first call. The return value tells the caller how much was accepted, so it knows that byte 08 is still its responsibility. The decoder's consumed count on Day 4 works the same way. Next, the checkpoint combines a handle, reset and destroy into one small library.
Completion is saved in this browser only. It does not measure mastery.