gpt4 book ai didi

c++ - 是否可以只对程序的一部分使用 Boehm 垃圾收集器?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:42:01 26 4
gpt4 key购买 nike

我读过 article in LinuxJournal关于Boehm-Demers-Weiser garbage collector library .我很感兴趣在我的库中使用它而不是我自己的引用计数实现。

我只有一个问题:是否可以只对我的共享库使用 gc 而在主应用程序中仍然使用 malloc/free?我不太明白 gc 如何检查堆,所以我担心 gc 在这种情况下的性能和可能的副作用。

最佳答案

example在手册状态下:

It is usually best not to mix garbage-collected allocation with the system malloc-free. If you do, you need to be careful not to store pointers to the garbage-collected heap in memory allocated with the system malloc.

更具体地针对 C++:

In the case of C++, you need to be especially careful not to store pointers to the garbage-collected heap in areas that are not traced by the collector. The collector includes some alternate interfaces to make that easier.

查看手册中的源代码,您会看到垃圾回收内存是通过特定调用处理的,因此,管理是单独处理的(由收集器或手动处理)。因此,只要您的库正确处理其内部结构并且不公开收集的内存,您应该没问题。您不知道其他库如何管理它们的内存,但您也可以使用它们,不是吗? :)

关于c++ - 是否可以只对程序的一部分使用 Boehm 垃圾收集器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2937822/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com