Hacker News new | past | comments | ask | show | jobs | submit login

I don't understand; why hang on to previously freed array space to reuse for new iterations of the array? Isn't the whole point of freeing array space to allow arbitrary other data to use it as needed?



Take a look here: https://en.wikipedia.org/wiki/Memory_management#DYNAMIC.

No one is really hanging on to previously freed space (except for the allocator, or if you're using a custom object pool allocation scheme) specifically for new versions of the array.

But if you're allocations look like this:

  array = malloc(<capacity>);
  // do stuff with array
  free(array);
  ...
  array = malloc(<new capacity>);
  // do stuff with array
  free(array);
with no other allocations in between then it is possible that the allocator might reuse previously freed array space.


I guess let me put my question another way: what's the advantage of being able to reuse previously freed array space for new iterations of the array, vs. having that space used for something else and using some other space for new iterations of the array?

It seems to me that, when reallocating, one ought to be able to say to the memory allocator "I want you to reallocate this array to a new size of [whatever]; go find a contiguous block of size [whatever], possibly overlapping the current array but not overlapping any other allocated memory, and copy/move the contents over there appropriately". (I believe that's what the "realloc" function does, no?). And, in that context, I can't see why any of the golden ratio stuff matters (though, of course, exponential resizing is still useful as noted).




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: