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

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

问题描述

我正在观看Bjarne Stroustrup的演讲 The Essential C ++

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



请问接口是什么,以及如何实现它?
更详细的在线好介绍,或者一些示例代码来证明它吗?

解决方案

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


垃圾回收(自动回收
内存的未引用区域)是可选的在C ++中;也就是说,垃圾收集器不是实现的
强制部分。但是, C ++ 11提供了一个
的定义,说明GC如果使用
和ABI(应用程序
二进制接口)可以执行什么操作来控制其操作。 p>

指针和生命期的规则用安全的
派生指针(3.7.4.3)表示。粗略地说:指向由new或其子对象分配
的东西的指针。
给普通凡人:[...]


C ++标准中的功能支持这个(接口其中Stroustrup指的是):



这些函数在 N2670建议


它的目的是同时支持垃圾收集的实现和
reachabil基于ity的泄漏检测器。这是通过给隐藏指针的程序提供未定义的
行为来完成的,例如,用另一个值对它
进行异或运算,然后再将其转换回普通的
指针并将其解除引用。这类程序目前可能会在保守的垃圾收集器中产生
的不正确结果,因为仅由这种隐藏指针引用的
对象可能会过早地收集
。出于同样的原因,基于可达性的泄漏检测器可能会
错误地报告这些程序泄漏内存。

您的实现支持严格的指针安全性在这种情况下可以实现GC,或者它具有轻松指针安全性(默认情况下),在这种情况下不是。您可以通过查看 std :: get_pointer_safety()的结果来确定结果, code> ,如果有的话。



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

I was watching Bjarne Stroustrup's talk "The Essential 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天全站免登陆