Search code examples
cmemory-managementmallocrealloc

Using realloc() for exact amount vs malloc() for too much


I have a bunch of files that I'm going to be processing in batches of about 1000. Some calculations are done on the files and approximately 75% of them will need to have data stored in a struct array.

I have no way of knowing how many files will need to be stored in the array until the calculations are done at runtime.

Right now I'm counting the number of files processed that need a struct array space, and using malloc(). Then for the next batch of files I use realloc(), and so on until all files are done. This way I allocate the exact amount of memory I need.

I'm able to count the total number of files in advance. Would I be better off using one big malloc() right at the start, even though it's only going to be 75% filled?


Solution

  • I would try it both ways, and see if there is a noticeable difference in performance.

    If there isn't I would stick with using realloc() as then you wont allocate any excess memory that you don't need. You can also do something like the vector class in C++ which increases the memory logarithmically.

    Libraries can implement different strategies for growth to balance between memory usage and reallocations, but in any case, reallocations should only happen at logarithmically growing intervals of size so that the insertion of individual elements at the end of the vector can be provided with amortized constant time complexity

    Keep in mind that you can also allocate memory in advance, and free some of it when you are done. It really depends on what your constraints are.