难道动态内存分配中流行的实现C和C ++有什么不同? [英] Does dynamic memory allocation differ in C and C++ in popular implementations?

查看:174
本文介绍了难道动态内存分配中流行的实现C和C ++有什么不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

至于各自的语言标准走,C只能通过的malloc()系列提供了动态内存分配,而在C ++中,则是由分配的最常见的形式 ::运算符new()。 C风格的malloc也是在C ++中可用,许多宝宝的第一次分配的例子用它作为核心配置的功能,但我很好奇如何编译器实现当代的实际生产经营者新的。

As far as the respective language standards go, C offers dynamic memory allocation only through the malloc() family, while in C++ the most common form of allocation is performed by ::operator new(). The C-style malloc is also available in C++, and many "baby's first allocator" examples use it as its core allocation function, but I am curious how contemporary compilers implement the actual production operator-new.

难道仅仅是围绕瘦包装的malloc(),或者是它相比,实现从根本上不同的是考虑一个典型的C ++程序的,而不同的内存分配行为典型的C程序?

Is it just a thin wrapper around malloc(), or is it implemented fundamentally differently on account of the rather different memory allocation behaviour of a typical C++ program compared to a typical C program?

[编辑:的我认为主要的区别通常被描述如下:C程序都有更少,更大,长寿命的分配,而一个C ++程序有很多,小的,短命的分配。随意附和,如果这是错误的,但它听起来就像人们从考虑到这一点获益。]

[ I believe the main difference is usually described as follows: A C program has fewer, larger, long-lived allocations, while a C++ program has many, small, short-lived allocations. Feel free to chime in if that's mistaken, but it sounds like one would benefit from taking this into account.]

对于像GCC编译器它会很容易,只是有一个单一的核心分配的实施和使用,对于所有相关的语言,所以我不知道是否有试图优化每一种语言所产生的分配性能细节上的差异。

For a compiler like GCC it would be easy to just have one single core allocation implementation and use that for all relevant languages, so I wonder if there are differences in the details that try to optimize the resulting allocation performance in each language.

更新:的感谢所有伟大的答案!它看起来像在海湾合作委员会由本的 ptmalloc 的是彻底解决,而MSVC还使用的malloc 为核心。有谁知道MSVC-的malloc是如何实现的?

Update: Thanks for all the great answers! It looks like in GCC this is completely solved by ptmalloc, and that MSVC also uses malloc at the core. Does anyone know how the MSVC-malloc is implemented?

推荐答案

下面是使用实施 G ++ 4.6.1

_GLIBCXX_WEAK_DEFINITION void *
operator new (std::size_t sz) throw (std::bad_alloc)
{
  void *p;

  /* malloc (0) is unpredictable; avoid it.  */
  if (sz == 0)
    sz = 1;
  p = (void *) malloc (sz);
  while (p == 0)
    {
      new_handler handler = __new_handler;
      if (! handler)
#ifdef __EXCEPTIONS
        throw bad_alloc();
#else
        std::abort();
#endif
      handler ();
      p = (void *) malloc (sz);
    }

  return p;
}

从V3 / libsupc ++ / new_op.cc 的G ++源发行内部

This is found in libstdc++-v3/libsupc++/new_op.cc inside the g++ source distro.

这是在的libstdc ++发现

正如你所看到的,它是围绕一个相当薄的包装的malloc

修改在很多系统中是可能的微调的malloc 的行为,通常是通过调用 mallopt 或设置环境变量。这里有一个文章,讨论一些在Linux上可用的功能。

edit On many systems it is possible to fine-tune the behaviour of malloc, typically by calling mallopt or setting environment variables. Here is one article discussing some features available on Linux.

根据维基百科的glibc 2.3+版本使用名为 ptmalloc 分配器,这本​​身就是一个修改后的版本dlmalloc 的衍生物 Doug Lea的设计。有趣的是,在大约一个文章 dlmalloc Doug Lea的给出了以下观点(重点煤矿):

According to Wikipedia, glibc versions 2.3+ use a modified version of the allocator called ptmalloc, which itself is a derivative of dlmalloc designed by Doug Lea. Interestingly, in an article about dlmalloc Doug Lea gives the following perspective (emphasis mine):

我写的分配器的第一个版本写一些C ++后
  上分配动态内存几乎完全依赖程序。
  我发现他们跑慢得多和/或更多的总
  内存占用比我预期他们。这是由于
  我上正在运行的系统内存分配器的特性
  上(主要是SunOS的和BSD的当时最新的版本)。反击
  这一点,首先我写C ++中的一些特殊用途的分配器,
  通常通过重载运营商新的各种课程。一些
  这些都是在纸上C ++分配技术,这是描述
  改编成1989年的C ++报告文章的一些存储分配
  技术容器类。

I wrote the first version of the allocator after writing some C++ programs that almost exclusively relied on allocating dynamic memory. I found that they ran much more slowly and/or with much more total memory consumption than I expected them to. This was due to characteristics of the memory allocators on the systems I was running on (mainly the then-current versions of SunOs and BSD ). To counter this, at first I wrote a number of special-purpose allocators in C++, normally by overloading operator new for various classes. Some of these are described in a paper on C++ allocation techniques that was adapted into the 1989 C++ Report article Some storage allocation techniques for container classes.

不过,我很快意识到,构建为每个特殊分配器
  动态分配是倾向于新类,并大量使用了
  建立各种通用的编程时,不是一个好策略
  支持类我当时写作。 (从1986年到1991年,我是
  的libg的GNU C ++库++的主要作者。)更广泛
  需要的解决方案 - 写的分配器,这是不够好
  在正常的C ++和C加载
,这样程序员会不动心
  写专用分配器除在非常特殊的
  条件。

However, I soon realized that building a special allocator for each new class that tended to be dynamically allocated and heavily used was not a good strategy when building kinds of general-purpose programming support classes I was writing at the time. (From 1986 to 1991, I was the the primary author of libg++ , the GNU C++ library.) A broader solution was needed -- to write an allocator that was good enough under normal C++ and C loads so that programmers would not be tempted to write special-purpose allocators except under very special conditions.

本文presents的一些主要设计目标的描述,
  算法,而对于这个分配器实现注意事项。

This article presents a description of some of the main design goals, algorithms, and implementation considerations for this allocator.

这篇关于难道动态内存分配中流行的实现C和C ++有什么不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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