C ++:内存分配器 [英] C++: Memory allocators

查看:104
本文介绍了C ++:内存分配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说有人在他们的项目中使用自定义内存分配器,特别是在C ++中.

  • 与malloc相比,什么是自定义内存分配器?

  • 不是已经分配了您可以进入的最低级别?

解决方案

内存分配器的级别不低于malloc. (默认分配器通常直接或间接调用malloc)

分配器仅允许您指定不同的分配策略.例如,您可以使用一个分配器,该分配器一次调用malloc来检索一个大的内存池,然后对于后续的分配请求,它仅返回该池的一小块.

或者您可以将其用作挂钩,以便每次分配或释放内存时都可以执行一些其他任务.

关于第二个问题,malloc是在不损失便携性的前提下可以做到的最低价格. malloc通常是使用某些特定于OS的内存分配功能实现的,因此该级别仍然较低.但这与您的主要问题无关,因为C ++分配器是更高级别的抽象.

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

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

  • Isn't malloc the lowest level you can go already?

解决方案

A memory allocator isn't lower level than malloc. (The default allocator typically calls malloc directly or indirectly)

An allocator just allows you to specify different allocation strategies. For example, you might use an allocator which calls malloc once to retrieve a large pool of memory, and then for subsequent allocation requests, it just returns a small chunk of this pool.

Or you may use it as a hook to allow you to perform some additional task every time memory is allocated or freed.

As to your second question, malloc is the lowest you can go without losing portability. malloc is typically implemented using some OS-specific memory allocation function, so that would be lower level still. But that's unrelated to your main question, since C++ allocators are a higher-level abstraction.

这篇关于C ++:内存分配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆