c++11:它的gc接口是什么,如何实现? [英] c++11: what is its gc interface, and how to implement?

查看:32
本文介绍了c++11:它的gc接口是什么,如何实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在观看 Bjarne Stroustrup 的演讲C++ 的本质".p>

在 44:26 中他提到了C++11 指定了一个 GC 接口".

请问什么是接口,如何实现?网上有没有更详细的好介绍,或者一些示例代码来演示一下?

解决方案

Stroustrup 在他的 中扩展了这个讨论C++ FAQ,问题是 GC 的使用是可选的,库供应商可以自由实现或不实现:

<块引用>

垃圾收集(自动回收未引用的区域memory) 在 C++ 中是可选;也就是说,垃圾收集器不是实施的强制性部分.然而,C++11 提供了一个定义如果使用 GC 可以做什么和 ABI(应用程序二进制接口)来帮助控制其操作.

指针和生命周期的规则用安全地派生指针"(3.7.4.3);粗略地说:指向分配的东西的指针通过新的或其子对象."对普通凡人:[...]

C++ 标准中支持这一点的函数(Stroustrup 所指的接口")是:

N2670 提案中介绍了这些功能:

<块引用>

它的目的是支持垃圾收集实现和基于可达性的检漏仪.这是通过给出未定义的通过例如异或来隐藏指针"的程序的行为用另一个值,然后再把它变回普通的指针并取消引用它.此类程序目前可能会产生保守垃圾收集器的结果不正确,因为仅由这种隐藏指针"引用的对象可能过早集.出于同样的原因,基于可达性的检漏仪可能错误地报告此类程序泄漏内存.

您的实现要么支持严格指针安全",在这种情况下实现 GC 是可能的,要么它具有宽松指针安全"(默认情况下),在这种情况下不支持.您可以通过查看 std::get_pointer_safety(),如果有的话.

我不知道任何实际的标准 C++ GC 实现,但至少 标准正在为它的发生做好准备.

I was watching Bjarne Stroustrup's talk "The Essence of C++".

In 44:26 he mentioned "C++11 specifies a GC Interface".

May I ask what is the interface, and how to implement it? Any more detailed good introduction online, or some sample codes to demonstrate it pls?

解决方案

Stroustrup extends this discussion in his C++ FAQ, the thing is that GC usage is optional, library vendors are free to implement one or not :

Garbage collection (automatic recycling of unreferenced regions of memory) is optional in C++; that is, a garbage collector is not a compulsory part of an implementation. However, C++11 provides a definition of what a GC can do if one is used and an ABI (Application Binary Interface) to help control its actions.

The rules for pointers and lifetimes are expressed in terms of "safely derived pointer" (3.7.4.3); roughly: "pointer to something allocated by new or to a sub-object thereof." to ordinary mortals: [...]

The functions in the C++ standard supporting this (the "interface" to which Stroustrup is referring to) are :

These functions are presented in the N2670 proposal :

Its purpose is to support both garbage collected implementations and reachability-based leak detectors. This is done by giving undefined behavior to programs that "hide a pointer" by, for example, xor-ing it with another value, and then later turn it back into an ordinary pointer and dereference it. Such programs may currently produce incorrect results with conservative garbage collectors, since an object referenced only by such a "hidden pointer" may be prematurely collected. For the same reason, reachability-based leak detectors may erroneously report that such programs leak memory.

Either your implementation supports "strict pointer safety" in which case implementing a GC is possible, or it has a "relaxed pointer safety" (by default), in which case it is not. You can determine that by looking at the result of std::get_pointer_safety(), if available.

I don't know of any actual standard C++ GC implementation, but at least the standard is preparing the ground for it to happen.

这篇关于c++11:它的gc接口是什么,如何实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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