Return to this page after a few days, and again about a week later. Recalling an idea from a blank editor, after you have started to forget it, is what makes it stick; rereading feels productive but fades quickly. Work without the earlier lessons open. When you get stuck, write down what you could not remember, then look it up. This review is extra practice outside Day 7's time estimate.
Retrieve
From a blank file, write a growable int collection with init, append and destroy. Then answer in writing, without running anything:
- Who owns
data, and which single function releases it? - After a rejected append, which fields may have changed?
- Which invariant makes
capacity - lengthsafe, and what would that subtraction do if the invariant were broken? - Why does an empty collection have
data == NULLinstead of a zero-byte allocation?
Debug
Each fragment below has one ownership bug. Explain it without running the code, because running it would be undefined behavior or a silent leak, and then write the repair.
list->data = realloc(list->data, capacity * sizeof *list->data);
if (list->data == NULL) { return STATUS_NOMEM; }On failure, the only pointer to the old block is overwritten with NULL: the records leak and the collection now claims to be empty while its length says otherwise. The repair receives the result in a temporary, returns on NULL, and assigns data and capacity together only on success.
int *first = &list.data[0];
list_append(&list, 50);
printf("%d\n", *first);If the append grew the block, first points into freed memory. Keep the index 0 instead and form &list.data[0] again after the append, or copy the value out before appending.
IntArray copy = array;
zeros_destroy(&array);
zeros_destroy(©);Both structs hold the same pointer, so the block is freed twice. Owning structs are passed by pointer, never copied.
Transfer
A new record type, Book, holds a title and a page count. Write a checked byte-count helper for any element size and test it with zero, ordinary counts and overflow. Then plan, in writing, how you would force two failures deterministically with a probe allocator: the very first allocation of an empty collection, and the growth from 4 to 8 elements. Say which fail_at value you would set, when you would set it, and what state you would check afterward.
Predict and run
Read the program before pressing Run and write down its output.
#include <stdio.h>
#include <stddef.h>
int main(void) {
size_t length = 4, capacity = 8;
printf("%zu\n", capacity - length);
return 0;
}Check your prediction
4Four more appends fit before the next growth. After a failed growth, verify that data, length and capacity match what they were before the call, and that the live-block count has not changed.
Practice
Your turn
Implement int safe_product(size_t count, size_t size, size_t *out). Reject size == 0 and any overflowing product without changing *out. Allow count == 0 to produce 0 bytes: this is only arithmetic, and the zero-size allocation policy is a separate decision. The program must print SIZE_MAX x 2: rejected, still 7, 2 x 0: rejected, still 7, 3 x 4: 12, 0 x 4: 0.
#include <stdio.h>
#include <stdint.h>
/* Write safe_product here. */
int main(void) {
size_t bytes = 7;
if (!safe_product(SIZE_MAX, 2, &bytes)) { printf("SIZE_MAX x 2: rejected, still %zu\n", bytes); }
if (!safe_product(2, 0, &bytes)) { printf("2 x 0: rejected, still %zu\n", bytes); }
if (safe_product(3, 4, &bytes)) { printf("3 x 4: %zu\n", bytes); }
if (safe_product(0, 4, &bytes)) { printf("0 x 4: %zu\n", bytes); }
return 0;
}Hint 1: the idea
A safe arithmetic result is separate from an allocator's zero-size policy. The helper answers "does this product fit?", and the collection decides whether to request it.
Hint 2: a first step
Write the version you would have written before this course, *out = count * size; return 1;, and run. Only the last two lines appear: both bad requests "succeed", and SIZE_MAX x 2 quietly stores a wrapped product instead of being rejected. Before writing the fix, decide which single comparison tells you a product will not fit without computing it, and what must be true about size before you can use it in that comparison.
Show one solution
#include <stdio.h>
#include <stdint.h>
int safe_product(size_t count, size_t size, size_t *out) {
if (size == 0 || count > SIZE_MAX / size) { return 0; }
*out = count * size;
return 1;
}
int main(void) {
size_t bytes = 7;
if (!safe_product(SIZE_MAX, 2, &bytes)) { printf("SIZE_MAX x 2: rejected, still %zu\n", bytes); }
if (!safe_product(2, 0, &bytes)) { printf("2 x 0: rejected, still %zu\n", bytes); }
if (safe_product(3, 4, &bytes)) { printf("3 x 4: %zu\n", bytes); }
if (safe_product(0, 4, &bytes)) { printf("0 x 4: %zu\n", bytes); }
return 0;
}SIZE_MAX x 2: rejected, still 7
2 x 0: rejected, still 7
3 x 4: 12
0 x 4: 0The || evaluates size == 0 first, so the division never divides by zero. Rejecting a zero size is a choice for this helper: no real element type has size 0. If ownership or realloc failure still feels uncertain, revisit Days 2 and 3 before building linked structures. More advanced C can explore opaque types, binary formats and operating-system interfaces once these contracts feel routine.
Completion is saved in this browser only. It does not measure mastery.