Skip to content

Commit ab60f2c

Browse files
committed
Merge branch 'as/api-allocation-doc' into maint
* as/api-allocation-doc: api-allocation-growing.txt: encourage better variable naming
2 parents d0f9456 + 5062f9e commit ab60f2c

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

Documentation/technical/api-allocation-growing.txt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,32 @@ Dynamically growing an array using realloc() is error prone and boring.
55

66
Define 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,
1719
alloc)`. This ensures that the array can hold at least `n` elements by
1820
calling `realloc(3)` and adjusting `alloc` variable.
1921

2022
------------
21-
sometype *ary;
23+
sometype *item;
2224
size_t nr;
2325
size_t alloc
2426

2527
for (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

3436
You are responsible for updating the `nr` variable.

0 commit comments

Comments
 (0)