C ++中的垃圾收集 - 为什么? [英] Garbage Collection in C++ -- why?

查看:113
本文介绍了C ++中的垃圾收集 - 为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不断听到人们抱怨C ++没有垃圾收集。我也听说C ++标准委员会正在把它添加到语言。我恐怕我只是没有看到它的点。使用RAII与智能指针,消除了它的需要,对吗?

I keep hearing people complaining that C++ doesn't have garbage collection. I also hear that the C++ Standards Committee is looking at adding it to the language. I'm afraid I just don't see the point to it... using RAII with smart pointers eliminates the need for it, right?

我唯一的经验与垃圾收集是在几个便宜的八十年代家用电脑,这意味着系统会冻结几秒钟每次都这样频繁。我相信自那以后它已经改善,但正如你可以猜到的,这并没有让我高度的意见。

My only experience with garbage collection was on a couple of cheap eighties home computers, where it meant that the system would freeze up for a few seconds every so often. I'm sure it has improved since then, but as you can guess, that didn't leave me with a high opinion of it.

垃圾回收的优点一个经验丰富的C ++开发人员?

What advantages could garbage collection offer an experienced C++ developer?

推荐答案

我不断听到有人抱怨C ++没有垃圾回收。



我很抱歉他们。认真。

I keep hearing people complaining that C++ doesn't have garbage collection.

I am so sorry for them. Seriously.

C ++有RAII,我总是抱怨在垃圾收集语言中找不到RAII(或阉割的RAII)。

C++ has RAII, and I always complain to find no RAII (or a castrated RAII) in Garbage Collected languages.

另一个工具。

Matt J在他的帖子中写得相当正确( C ++中的垃圾回收 - 为什么?):我们不需要C ++特性,因为它们大多数可以用C编码,我们不需要C特性,因为大多数特性可以在Assembly等中编码。 C ++必须演变。

Matt J wrote it quite right in his post (Garbage Collection in C++ -- why?): We don't need C++ features as most of them could be coded in C, and we don't need C features as most of them could coded in Assembly, etc.. C++ must evolve.

作为开发人员:我不在乎GC。我试过RAII和GC,我发现RAII非常优越。正如Greg Rogers在他的帖子中所说( C ++中的垃圾回收 - 为什么?),内存泄漏不是那么可怕(至少在C ++中,如果C ++真的被使用,它们是罕见的),以证明GC而不是RAII。 GC具有非确定性的释放/最终化,只是编写一个不关心特定内存选择的代码

As a developer: I don't care about GC. I tried both RAII and GC, and I find RAII vastly superior. As said by Greg Rogers in his post (Garbage Collection in C++ -- why?), memory leaks are not so terrible (at least in C++, where they are rare if C++ is really used) as to justify GC instead of RAII. GC has non deterministic deallocation/finalization and is just a way to write a code that just don't care with specific memory choices.

最后一句很重要:编写juste do not care的代码很重要。以同样的方式在C + + RAII我们不关心ressource释放,因为RAII它为我们,或对象初始化,因为构造函数为我们,有时重要的是,只是代码不关心谁是什么内存的所有者,和什么样的指针(共享,弱等)我们需要这个或这段代码。 似乎需要在C ++中使用GC。(即使我没有看到它)

This last sentence is important: It is important to write code that "juste don't care". In the same way in C++ RAII we don't care about ressource freeing because RAII do it for us, or for object initialization because constructor do it for us, it is sometimes important to just code without caring about who is owner of what memory, and what kind pointer (shared, weak, etc.) we need for this or this piece of code. There seems to be a need for GC in C++. (even if I personaly fail to see it)

有时,在应用程式中,您有「浮动资料」。想象一下树状数据结构,但没有人真正是数据的所有者(没有人真正关心它何时被破坏)。多个对象可以使用它,然后,丢弃它。您希望在没有人使用它时释放它。

Sometimes, in an app, you have "floating data". Imagine a tree-like structure of data, but no one is really "owner" of the data (and no one really cares about when exactly it will be destroyed). Multiple objects can use it, and then, discard it. You want it to be freed when no one is using it anymore.

C ++方法使用智能指针。 boost :: shared_ptr想起来了。所以每个数据都由自己的共享指针拥有。凉。问题是当每条数据可以引用另一条数据时。您不能使用共享指针,因为它们使用引用计数器,它不支持循环引用(A指向B,B指向A)。所以你必须知道在很多地方使用弱指针(boost :: weak_ptr),以及何时使用共享指针。

The C++ approach is using a smart pointer. The boost::shared_ptr comes to mind. So each piece of data is owned by its own shared pointer. Cool. The problem is that when each piece of data can refer to another piece of data. You cannot use shared pointers because they are using a reference counter, which won't support circular references (A points to B, and B points to A). So you must know think a lot about where to use weak pointers (boost::weak_ptr), and when to use shared pointers.

使用GC,你只需使用树结构数据。

With a GC, you just use the tree structured data.

缺点是你不必关心何时浮动数据将被真正销毁。

The downside being that you must not care when the "floating data" will really be destroyed. Only that it will be destroyed.

因此,如果做得很好,并且与C ++的当前习语兼容,GC将是又一个好的C ++工具

So in the end, if done properly, and compatible with the current idioms of C++, GC would be a Yet Another Good Tool for C++.

C ++是一个多属性语言:添加一个GC可能会使一些C ++ fanboys因为叛国而哭泣,但最终,这可能是一个好主意,我猜C ++标准Comitee不会让这种主要特性打破语言,所以我们可以信任他们进行必要的工作,以使一个正确的C ++ GC,不会干扰C ++:像C ++一样,如果你不需要一个功能,不要使用它,它会花费你什么。

C++ is a multiparadigm language: Adding a GC will perhaps make some C++ fanboys cry because of treason, but in the end, it could be a good idea, and I guess the C++ Standards Comitee won't let this kind of major feature break the language, so we can trust them to make the necessary work to enable a correct C++ GC that won't interfere with C++: As always in C++, if you don't need a feature, don't use it and it will cost you nothing.

这篇关于C ++中的垃圾收集 - 为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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