I use dynamic arrays all the time courtesy of NSMutableArray, but I realized today that I wasn't super sure how to implement an actual low level dynamically growing C array.
I was aware that a common method of doing so was to create an array of size 1 and then to just add stuff to that array. The only caveat is that if, when adding a new element, you've filled up your array, on the next insertion you'll need to double the size of the array. The significant thing about this strategy is that the doubling operation is classic log(n) growth. How many times will you need to do this doubling to have an array that allows 1,000,000 elements? Something like 20, I don't remember the exact number but you've got a calculator right in front of you.
Anyway, that part seems pretty straightforward, but how exactly do you double the size of an array in C? Well turns out that's easy too. You just use realloc() which will take a pointer to an array and return a new pointer to an array of a new size with your elements copied over from the first one.
The interesting thing here, is that whether or not the array you get back happens to start at the same pointer location depends on whether the system can pull that off or not. You always need to use a new pointer to catch whatever gets returned though. If instead you were to always overwrite the old pointer with the new one then any time it is actually a new location in memory, the old array will be lost in space and you won't be able to free it.