4 Dynamically growing an array using realloc() is error prone and boring.
6 Define your array with:
8 * a pointer (`ary`) that points at the array, initialized to `NULL`;
10 * an integer variable (`alloc`) that keeps track of how big the current
11 allocation is, initialized to `0`;
13 * another integer variable (`nr`) to keep track of how many elements the
14 array currently has, initialized to `0`.
16 Then before adding `n`th element to the array, call `ALLOC_GROW(ary, n,
17 alloc)`. This ensures that the array can hold at least `n` elements by
18 calling `realloc(3)` and adjusting `alloc` variable.
25 for (i = 0; i < nr; i++)
26 if (we like ary[i] already)
29 /* we did not like any existing one, so add one */
30 ALLOC_GROW(ary, nr + 1, alloc);
31 ary[nr++] = value you like;
34 You are responsible for updating the `nr` variable.