pointers-memory · intermediate · ~30 min
Doubling growth strategy and the realloc idiom (use a temporary so OOM doesn't leak the original).
Implement a growable byte buffer:
typedef struct { char *data; size_t len, cap; } buf_t;
void buf_init(buf_t *b);
int buf_append(buf_t *b, const char *bytes, size_t n); /* returns 0 on success, -1 on OOM */
void buf_free(buf_t *b);
On growth, double capacity (start at 8).
A growable buffer is the kernel of every dynamic string / vector / IO read loop in C. The doubling-growth strategy is what makes append O(1) amortised.
bytes pointer, n bytes to append.
0 / -1.
Use realloc, not malloc+memcpy+free.
#include <stddef.h>
#ifndef BUF_T_DEFINED
#define BUF_T_DEFINED
typedef struct { char *data; size_t len, cap; } buf_t;
#endif
void buf_init(buf_t *b);
int buf_append(buf_t *b, const char *bytes, size_t n);
void buf_free(buf_t *b);
p = realloc(p, n) — if realloc returns NULL, you lose the old pointer and leak. Growth by +n (gives O(n^2) total appends). Forgetting to update both len and cap.
First append from cap=0 — need an initial size (typically 8). Append of 0 bytes — no-op. Append larger than current capacity — may need multiple doublings.
O(1) amortised per byte. Total O(n) for n bytes across O(log n) reallocs.
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.