Disadvantages of calling realloc in a loop

雨燕双飞 提交于 2019-12-24 04:02:32

问题


I'm trying to implement some math algorithms in C on Windows 7, and I need to repeatedly increase size of my array.

Sometimes it fails because realloc can't allocate memory. But if I allocate a lot of memory at once in the beginning it works fine.

Is it a problem with the memory manager? Can anyone explain me this please?


回答1:


  1. When you allocate/deallocate memory many times, it may create fragmentation in the memory and you may not get big contiguous chunk of the memory.
  2. When you do a realloc, some extra memory may be needed for a short period of time to move the data.

If your algorithm does not need contiguous memory or can be changed to work on non-contiguous memory, consider using linked-lists of array (Something link std::dequeue of C++) that will avoid copying of data and your code may not suffer OOM. If you know the worst case memory requirement for the array, it is better to keep that memory allocated from the beginning itself, as it will avoid the cost of allocation and data moving when compared with realloc.




回答2:


I you want your algorithms to work fast, try to do all the memory allocation up front. Memory allocation is an unbounded operation and will kill your performance. So speculate a reasonable worst case and allocate enough for that. If you do need to realloc later fine but don't do so continuously.



来源:https://stackoverflow.com/questions/30976295/disadvantages-of-calling-realloc-in-a-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!