ident: do not drop username when reading from /etc/mailname
[git/jnareb-git.git] / Documentation / technical / api-allocation-growing.txt
blob43dbe09f735525b0a1549ccfb4de2f2ca87252a0
1 allocation growing API
2 ======================
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.
20 ------------
21 sometype *ary;
22 size_t nr;
23 size_t alloc
25 for (i = 0; i < nr; i++)
26         if (we like ary[i] already)
27                 return;
29 /* we did not like any existing one, so add one */
30 ALLOC_GROW(ary, nr + 1, alloc);
31 ary[nr++] = value you like;
32 ------------
34 You are responsible for updating the `nr` variable.