什么是"正确"办法调和malloc和新的混合的C / C ++程序? [英] What is the "correct" way to reconcile malloc and new in a mixed C/C++ program?

查看:104
本文介绍了什么是"正确"办法调和malloc和新的混合的C / C ++程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个混合的C / C ++程序。它包含一个Flex /野牛解析器针对C,而其余的是C ++。

I have a mixed C/C++ program. It contains a flex/bison parser which targets C, while the remainder is C++.

为C,生成的解析器和扫描器管理他们的记忆与的malloc 的realloc 免费。他们是不够好,挂接,让我提出的这些功能​​我自己的实现。正如你所预料的,(C ++)程序的其余部分要用删除等。

Being C, the generated parser and scanner manage their memory with malloc, realloc and free. They are good enough to expose hooks allowing me to submit my own implementations of these functions. As you might expect, the rest of the (C++) program "wants" to use new, delete, etc.

做了一些研究似乎表明,相关标准不保证这种混合应该工作。特别是C堆的不一定是C ++的自由区。看来这两个方案可以践踏对方。

Doing a little research seems to show that the relevant standards do not guarantee that such mixing should work. Particularly the C "heap" is not necessarily the C++ "free area". It seems the two schemes can trample each other.

在此之上,总有一天(不久)这一计划可能会想定制堆实现整合,如的 tcmalloc ,使用C和C ++。

On top of this, someday (soon) this program will probably want to integrate a customized heap implementation such as tcmalloc, used by both C and C++.

什么是正确的事在这里做什么?

由于整合tcmalloc的欲望(这也解释了如何使用C程序链接)我很想找到一些交叉型,跨线程,跨一切过载/钩/不管成C ++内存管理。随着我可以指出所有C ++的分配/释放回调于C当量(其中在tcmalloc又将土地上。)

Given the desire to integrate tcmalloc (which explains how to link with C programs) I'm tempted to find some cross-type, cross-thread, cross-everything overload/hook/whatever into C++ memory management. With that I could point all C++ allocation/release calls back to their C equivalents (which in turn land on tcmalloc.)

这样的泛银河全球C ++钩子是否存在?可能它已经在做我想要的东西,类似于如何的ios_bas​​e :: sync_with_stdio 偷偷默认结婚的iostream和标准输入输出?

Does such a pan-galactic global C++ hook exist? Might it already be doing what I want, similar to how ios_base::sync_with_stdio secretly marries iostream and stdio by default?

我没有兴趣谈论的stdio与输入输出流,也无法左右切换解析器生成器也使用C ++的Flex /野牛骨骼(他们引入独立的头痛。)

I am not interested in talking about stdio vs. iostreams, nor about switching parser generators nor using the C++ flex/bison skeletons (they introduce independent headaches.)

修改:请包括支持你的答案C ++标准的这些部分的名称

EDIT: Please include the names of those sections of the C++ standard that support your answer.

推荐答案

标准的确实的保证二者混合均匀分配变型将工作。它的的许可证是一样的东西叫免费上是从哪里来的内存的,因为它们可以使用一个完全不同的舞台的两种类型的

The standard does guarantee that mixing the two allocation variants will work. What it doesn't permit is things like calling free on memory that came from new, since they may use a totally different arena for the two types.

提供你还记得呼吁给定内存块的正确释放功能,您将被罚款。他们的不会的践踏对方,如果你遵守规则,如果你不遵守规则,那么,在技术上,的的做践踏,不是他们: - )

Providing you remember to call the correct deallocation function for a given block of memory, you will be fine. They won't trample each other if you follow the rules and, if you don't follow the rules then, technically, you're doing the trampling, not them :-)

在C ++ 11标准的控制部分是 13年6月20日C库其中指出,转述:

The controlling part of the C++11 standard is 20.6.13 C library which states, paraphrased:


  • 功能释放calloc 的malloc 免费的realloc 提供,基于C标准。

  • 的功能不使用 ::运算符new() ::运算符delete()

  • 这使得传统Ç东西使用不同内存的舞台那么普通的C ++内存分配。

  • The functions calloc, malloc, free and realloc are provided, based on the C standard.
  • The functions do not use ::operator new() or ::operator delete().
  • This allows the heritage C stuff to use a different memory arena then the normal C++ memory allocation.

这是第二个项目符号点中的什么你最终提议,放弃在 tcmalloc 来取代的C传承功能,并有C ++使用它,以及光有意思。

That second bullet point is interesting in light of what you're eventually proposing, dropping in tcmalloc to replace the C heritage functions and have C++ use it as well.

有在这解释了为什么他们不使用让的malloc()通话 ::运算符new()

There's a footnote in the standard which explains why they don't use let malloc() call ::operator new():

这样做的目的是通过调用的std ::的malloc()或std ::释放calloc()有运营商新的()实现的。换句话说,他们希望避免循环依赖。

The intent is to have operator new() implementable by calling std::malloc() or std::calloc(). In other words, they want to avoid a circular dependency.

不过,虽然它的允许运算符new()调用的malloc() ,我不知道,该标准实际上要求的它。所以,为了安全起见,你可能要注入 tcmalloc 到这两个的C的的C ++领域。

However, while it allows operator new() to call malloc(), I'm not sure that the standard actually requires it. So, to be safe, you'd probably want to inject tcmalloc into both the C and C++ areas.

您已经表明您已经知道该怎么做了C.对于C ++,它可以通过简单地提供一整套全球的完成运营商新的()/删除()在code函数,适当写入调用 tcmalloc 在幕后。 C ++标准州 3.7.4动态存储时间

You've indicated you already know how to do that for C. For C++, it can be done by simply providing the entire set of global operator new()/delete() functions in your code, suitably written to call tcmalloc under the covers. The C++ standard states in 3.7.4 Dynamic storage duration:

该库提供全球分配和释放功能默认定义。一些全球性分配和释放功能是可替换的。

The library provides default definitions for the global allocation and deallocation functions. Some global allocation and deallocation functions are replaceable.

一个C ++程序应提供可更换的分配或释放功能最多一个定义。任何这样的函数定义会替换
  默认版本库中提供的。

A C++ program shall provide at most one definition of a replaceable allocation or deallocation function. Any such function definition replaces the default version provided in the library.

下面的分配和释放功能,在程序的每个翻译单元全球范围内隐式声明:

The following allocation and deallocation functions are implicitly declared in global scope in each translation unit of a program:


      
  • 无效*运算符新(的std ::为size_t);

  •   
  • 无效*运营商新的[](的std ::为size_t);

  •   
  • 无效的operator delete(void *的);

  •   
  • 无效的operator delete [](void *的);

  •   
  • void* operator new(std::size_t);
  • void* operator new[](std::size_t);
  • void operator delete(void*);
  • void operator delete[](void*);

这篇关于什么是"正确"办法调和malloc和新的混合的C / C ++程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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