@@ -5,30 +5,32 @@ Dynamically growing an array using realloc() is error prone and boring.
55
66Define your array with:
77
8- * a pointer (`ary`) that points at the array, initialized to `NULL`;
8+ * a pointer (`item`) that points at the array, initialized to `NULL`
9+ (although please name the variable based on its contents, not on its
10+ type);
911
1012* an integer variable (`alloc`) that keeps track of how big the current
1113 allocation is, initialized to `0`;
1214
1315* another integer variable (`nr`) to keep track of how many elements the
1416 array currently has, initialized to `0`.
1517
16- Then before adding `n`th element to the array , call `ALLOC_GROW(ary , n,
18+ Then before adding `n`th element to the item , call `ALLOC_GROW(item , n,
1719alloc)`. This ensures that the array can hold at least `n` elements by
1820calling `realloc(3)` and adjusting `alloc` variable.
1921
2022------------
21- sometype *ary ;
23+ sometype *item ;
2224size_t nr;
2325size_t alloc
2426
2527for (i = 0; i < nr; i++)
26- if (we like ary [i] already)
28+ if (we like item [i] already)
2729 return;
2830
2931/* we did not like any existing one, so add one */
30- ALLOC_GROW(ary , nr + 1, alloc);
31- ary [nr++] = value you like;
32+ ALLOC_GROW(item , nr + 1, alloc);
33+ item [nr++] = value you like;
3234------------
3335
3436You are responsible for updating the `nr` variable.
0 commit comments