Calling realloc
on every append to your array is probably too expensive. You should keep the allocated size of your memory zone, and increase it once in a while. If oldsize
is the old size, you might compute something like newsize = (5*oldsize/4+10)|0xf;
since for small sizes this would grow by at least 10 elements, and for large sizes you won't lose more than about 25% of memory.
If speed is a concern, you might special-case the common small sizes, e.g. by declaring a local array int loc[8];
and use loc
instead of a malloc
-ed or calloc
-ed pointer for small arrays of size <=8. You could also consider alloca(3) to allocate some small array on the stack (don't use alloca
on more than a few dozens of kilobytes on current desktops), or use VLAs. You might also consider flexible array members.
Of course you should always test against failure of calls to malloc
, calloc
, realloc
etc.... (and in general of every library function you are using). Read carefully the documentation of malloc(3)