One big malloc versus several smaller reallocks

Sorry, if this was asked earlier, I could not find what I was looking for.

I read the fields from the list and write them to the memory block. I could

  • Go through the whole list, find the size you need, make one malloc and THEN go to the list again and copy each field;
  • Go through the whole list and realloc block of memory when I write the values;

Right now, the first seems to me the most efficient (the least number of calls). What are the pros and cons of any approach?

Thank you for your time.

+1
source share
3 answers

You are probably better off assigning a decent amount of space initially, based on what you think is most likely.

Then, if you find that you need more space, do not just allocate enough for additional ones, select a large piece in addition.

This will minimize the number of redistributions while processing only one list at a time.

As an example, we initially select 100K. If you need more then reinstall to 200K, even if you only need 101K.

+1
source

The first approach is almost always better. Realloc () usually works by copying the entire contents of a block of memory into a larger block just allocated. Thus, n reallocs can mean n copies, each of which is larger than the last. (If you add m bytes to your distribution each time, then the first realloc should copy m bytes, next 2m, next 3m, ...).

The pedantic answer will be that the internal consequences of realloc () are implementation-specific, not specifically defined by the standard, in some implementations it can work as fairy fairies that instantly move bytes, etc. etc., but in any realistic implementation, realloc () means copy.

+2
source

Do not reinvent the wheel and use CCAN darray , which implements an approach similar to that described by paxdiablo. See darray on github

0
source

All Articles