c++11:它的gc接口(interface)是什么,如何实现?

标签 c++11 garbage-collection

我在看 Bjarne Stroustrup 的演讲“The Essence of C++”。

在 44:26 他提到“C++11 指定了一个 GC 接口(interface)”。

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

最佳答案

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

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: [...]

支持此功能的 C++ 标准中的函数(Stroustrup 所指的“接口(interface)”)是:

这些功能在 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.

要么您的实现支持“严格指针安全”(在这种情况下可以实现 GC),要么它具有“宽松指针安全”(默认情况下),在这种情况下则不可能。您可以通过查看 std::get_pointer_safety() 的结果来确定,如果可用的话。

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

关于c++11:它的gc接口(interface)是什么,如何实现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48011044/

相关文章:

c++ - Visual Studio 编译并检测操作符重载,而 g++ 则不然

c++ - 模拟一堆卡片的最佳容器

arrays - 随着分配更多的盒装数组,代码变得更慢

java - 了解使用 map 和 vector 时垃圾收集器在 Java 7 中的工作原理

c++ - 'help' 编译器如何从作为函数的模板参数推导出函数模板返回类型?

c++ - 具有常量值的任何模板化类型的类型特征

c++ - 右值引用被视为左值?

Java内存泄漏和垃圾收集,

javascript - gsap tweenlite/tweenmax 垃圾收集、引用和性能

java - 确定 JVM 垃圾收集器的运行时间