How To Reallocate Memory

Chris Taylor has a nice post on reallocating arrays. The problem is a common one: you initially allocate an array (or other data structure) and later want to make it larger. In C, for example, you would use the realloc function. The problem at hand is how much additional memory to request with the reallocation. You’d like to arrange things so that some of the memory can be reused in further reallocations. Taylor explains exactly what this means in his post.

The theoretical answer turns out be that you should increase the current allocation by a factor of ≈1.618 or, to be precise, by the golden ratio. As a practical matter, a factor of 1.5 is probably a good value. None of this is of great import, of course, but the mathematician in me was charmed by the result. The golden ration keeps popping up in unexpected places.

This entry was posted in Programming. Bookmark the permalink.