gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 22:53:46 43 4
gpt4 key购买 nike

我正在观看 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/27728142/

43 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com