gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:41:59 27 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/48011044/

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