C++: Memory allocators

前端 未结 5 1160
情话喂你
情话喂你 2020-12-25 15:51

I\'ve heard about people using custom memory allocators for their project, particulary in C++.

  • What is a custom memory allocator, compared to malloc?

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-25 16:12

    A custom memory allocator is a replacement for malloc (actually, usually a replacement for operator new) that retrieves blocks of bytes in some fashion other than the default. malloc is not the lowest level you can go, because malloc itself is implemented in terms of even simpler primitives from the OS that allocate blocks of memory for partitioning.

    Common use cases for making custom allocators are optimizing for allocations of small objects (the default allocator is usually really bad at this), allocating in a way that guarantees good locality (by allocating objects near one another), allocating with logging/tracking (to diagnose leaks), allocating from a garbage-collected resource pool, etc. There are a lot of different options available, and many programs can squeeze out a bit more performance using these custom allocators.

提交回复
热议问题