Which algorithm is used in List to dynamically allocate memory?

后端 未结 6 1342
渐次进展
渐次进展 2021-01-18 22:18

Now I have an algorithm for dynamically allocating memory on an array:

  • If array is full I create a new array of twice the size, and copy items.
  • If arr
6条回答
  •  半阙折子戏
    2021-01-18 23:11

    Unless you have a specific reason to believe otherwise, it's almost universally a good idea to use the provided libraries that come with C#. Those implementations are well-implemented, well-debugged, and well-tested.

    The data structure you're describing is a standard implementation of a dynamic array data structure, and most languages use this as their default list implementation. Looking over the documentation for List, it seems like List uses this implementation, since its documentation has references to an internal capacity and guarantees O(1) append as long as the size is less than the capacity.

    In short, avoid reinventing the wheel unless you have to.

    Hope this helps!

提交回复
热议问题