@@ -5,30 +5,32 @@ Dynamically growing an array using realloc() is error prone and boring.
5
5
6
6
Define your array with:
7
7
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);
9
11
10
12
* an integer variable (`alloc`) that keeps track of how big the current
11
13
allocation is, initialized to `0`;
12
14
13
15
* another integer variable (`nr`) to keep track of how many elements the
14
16
array currently has, initialized to `0`.
15
17
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,
17
19
alloc)`. This ensures that the array can hold at least `n` elements by
18
20
calling `realloc(3)` and adjusting `alloc` variable.
19
21
20
22
------------
21
- sometype *ary ;
23
+ sometype *item ;
22
24
size_t nr;
23
25
size_t alloc
24
26
25
27
for (i = 0; i < nr; i++)
26
- if (we like ary [i] already)
28
+ if (we like item [i] already)
27
29
return;
28
30
29
31
/* 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;
32
34
------------
33
35
34
36
You are responsible for updating the `nr` variable.
0 commit comments