有没有比malloc/calloc更好的实现,可以在C中分配内存? [英] Is there any better implementation than malloc/calloc for allocating memory in C?

查看:98
本文介绍了有没有比malloc/calloc更好的实现,可以在C中分配内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为malloc/calloc是stdlib中为C定义的旧实现. 是否有任何更好的实现可以代替malloc/calloc ? 请不要在malloc/calloc方法上回答任何包装方法. 在C中分配内存的新实现不应调用 malloc/calloc ,而应实现自己的方法.

As malloc/calloc is the old implementation defined in stdlib for C . Is there any better implementation that can replace malloc/calloc ? Please don't answer any wrapper method on malloc/calloc method. The new implementation to allocate memory in C should not call malloc/calloc rather than it should implement its own method .

推荐答案

除非您的应用程序是多线程的,否则您可能不必理会. 本文(至少对于一个平台)建议malloc在竞争中表现出色在单线程环境中.

Unless your application is multithreaded you probably shouldn't bother. This article suggests (for one platform, at least) that malloc performs quite competitively in a single threaded environment.

那么,您应该在应用程序中使用哪个分配器?事实证明,对于单线程代码,Solaris OS上的malloc是一个不错的选择.

So, which allocator should you use in your application? malloc on the Solaris OS was shown to be a strong choice for single-threaded code.

您将很难做到比malloc或它的非常相似的功能更好.如果想要更好的内存分配,可以包装malloc或调用操作系统内存分配函数(但会丢失可移植性),然后将自己的分配逻辑放在内存块的顶部.

You'll be hard pressed to do better than malloc or its very similar functions. If you want better memory allocation you can wrap malloc or call the operating system memory allocation functions (but lose out on portability) and place your own allocation logic on top of chunks of memory.

如果您真的需要更好的内存分配器,请尝试>积:

If you really need a better memory allocator, try Hoard:

Hoard内存分配器是适用于Linux,Solaris,Mac OS X和Windows的快速,可扩展且内存高效的内存分配器. Hoard是malloc的直接替代品,可以显着提高应用程序性能,尤其是对于在多处理器和多核CPU上运行的多线程程序.

The Hoard memory allocator is a fast, scalable, and memory-efficient memory allocator for Linux, Solaris, Mac OS X, and Windows. Hoard is a drop-in replacement for malloc that can dramatically improve application performance, especially for multithreaded programs running on multiprocessors and multicore CPUs.

请注意,它不一定免费.如果您想要免费的,请尝试其中之一.

Note that it's not necessarily free. If you want a free one, try one of these.

这篇关于有没有比malloc/calloc更好的实现,可以在C中分配内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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