简体   繁体   English

C ++有垃圾收集器吗?

[英]Does C++ have a Garbage Collector?

I'm currently reading an unreleased master thesis report, that I'm going to give feedback on. 我正在阅读未发表的硕士论文报告,我将提供反馈意见。

In the report they mention GC under native C++ and managed C++. 在报告中,他们在本机C ++和托管C ++下提到了GC。 I thought C++ didn't have any standard GC, am I wrong or right? 我以为C ++没有任何标准的GC,我错了还是对的? (They do not mention Boehm-Demers-Weiser.) (他们没有提到Boehm-Demers-Weiser。)

They have some problem getting it to work under some conditions. 他们在某些条件下使其工作有一些问题。 They create objects in one thread, and delete the pointer from another thread. 它们在一个线程中创建对象,并从另一个线程中删除指针。

Managed C++ (and its successor C++/CLI) of course use .NET garbage collection for managed resources (though native resources are not garbage collected and have to be managed manually as in native C++). 托管C ++(及其后继者C ++ / CLI)当然使用.NET垃圾收集来管理资源(尽管本机资源不是垃圾收集的,必须像在本机C ++中那样手动管理)。

Native C++ by default has no such thing (the closest thing to this are the smart pointers, but that's still something entirely different), but that doesn't prevent you from writing your own garbage collection solution (or using third party solution ). 默认情况下,本机C ++没有这样的东西(最接近它的是智能指针,但这仍然是完全不同的东西),但这并不妨碍您编写自己的垃圾收集解决方案(或使用第三方解决方案 )。

Existing C++ standard of 1998/2004 does not specify a garbage collector. 1998/2004年现有的C ++标准没有指定垃圾收集器。 The upcoming standard C++0x does specify an optional garbage collector API, however the implementation is an other part. 即将推出的标准C ++ 0x确实指定了可选的垃圾收集器API,但实现是另一部分。 With all that said, there are garbage collectors available for C++ from compiler vendors and third party. 尽管如此,编译器供应商和第三方都有可用于C ++的垃圾收集器。

  • GCC suite provides Boehm-GC for garbage collection. GCC套件提供Boehm-GC用于垃圾收集。
  • Managed C++ was Microsoft's extension to C++ released with .Net 1.0 which extended C++ with garbage collection capabilities. 托管C ++是微软对.Net 1.0发布的C ++的扩展,它扩展了C ++的垃圾收集功能。
  • There is also C++/CIL from Microsoft released with .Net 2 which deprecated Managed C++ with more .Net centric features. 微软也发布了C ++ / CIL,其中包含.Net 2,它不推荐使用更多以.Net为中心的托管C ++。
  • Sun Provided libgc as garbage collector for C/C++. Sun提供了libgc作为C / C ++的垃圾收集器。

I have a different take on GC under C++. 我对C ++下的GC有不同的看法。

Have a look at MCP (open source license GPLv3) -- GC with C++ should not have to be any worse than GJC / Mono. 看看MCP (开源许可证GPLv3) - 使用C ++的GC不应该比GJC / Mono差。

C和C ++唯一的GC库是Hans-Boehm库 ,但据我所知很难设置它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM