Time complexity of memory allocation

后端 未结 5 700
旧时难觅i
旧时难觅i 2020-11-27 03:27

What is the time complexity of dynamic memory allocation using new, malloc, etc.? I know very little about how memory allocators are implemented, but I assume the answer is

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-27 04:02

    Just check out how typical allocators work.

    A bump-the-pointer allocator works in O(1), and it's a small '1' at that.

    For a segregated-storage allocator, allocation of k bytes means "return the first block from List(n)" where List(n) is the list of blocks of n bytes where n >= k. It might find that List(n) is empty, so that a block from the next list (List(2n)) would have to be split with both resulting blocks of n bytes being put onto List(n), and this effect might ripple through all availlable sizes, making for a complexity of O(ns) where ns is the number of different sizes availlable, and ns = log (N) where N is the size of the largest block size availlable, so even that would be small. In most cases, especially after a number of blocks have been allocated and deallocated, complexity is O(1).

提交回复
热议问题