Growing a buffer with realloc
The buffer starts with room for 2 numbers. When count reaches cap, realloc doubles the room. Watch the heap: the numbers move into a new, bigger block and the old block is gone.
The result goes into bigger first. If realloc failed it would return NULL, and writing straight into buf would lose the only pointer to the old block.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int cap = 2;
int count = 0;
int *buf = malloc(cap * sizeof *buf);
if (!buf) return 1;
for (int v = 1; v <= 5; v++) {
if (count == cap) {
cap *= 2;
int *bigger = realloc(buf, cap * sizeof *buf);
if (!bigger) { free(buf); return 1; }
buf = bigger;
}
buf[count++] = v * v;
}
printf("%d values, capacity %d\n", count, cap);
free(buf);
return 0;
}
Output:
5 values, capacity 8
From the lesson: Dynamic memory