Skip to main content

Checkpoint: Transfer + Failure-Safe Fill

Challenge on lesson: Checkpoint: Transfer and Survive Failure

CHALLENGE
Difficulty: advanced+25 XP

A file-scope int vecs_alive = 0; is in your editor. Implement: ``c typedef struct { int *buf; size_t len; size_t cap; } vec_t; int vec_push(vec_t *v, int value); void vec_free(vec_t *v); void vec_move(vec_t *dst, vec_t *src); int vec_fill(vec_t *v, int n, int fail_at); ` vec_push doubles capacity (starting at 1) and counts as an allocation: while v->buf is non-NULL it counts one toward vecs_alive โ€” simplest honest model: vec_push sets vecs_alive = 1 the first time it allocates for this vec. vec_free releases and hollows. vec_move transfers and hollows the source (without changing the counter โ€” ownership moved, the count did not). vec_fill pushes 1..n; if the push that would be number fail_at` (1-based) cannot proceed because a simulated allocation failure occurs there, free everything and return -1; success returns 0. Simulate the failure deterministically: fail exactly when the next index equals fail_at, before allocating.

Back to lesson: Checkpoint: Transfer and Survive Failure