C ++ 11: what is its gc interface and how to implement it?

I watched Bjarne Straustrup say " The Essential C ++ .

At 44:26 he mentioned "C ++ 11 indicates the GC interface."

May I ask what an interface is and how to implement it? Any more in-depth introduction online, or some code examples to demonstrate this pls?

+7
garbage-collection c ++ 11
source share
2 answers

Stroustrup expands this discussion in its frequently asked C ++ questions , the fact is that using GC is optional, library providers can implement one or not:

Garbage collection (automatic recycling of unregistered regions of memory) is optional in C ++; that is, the garbage collector is not a mandatory part of the implementation. However, C ++ 11 provides a definition of what the GC can do if it uses the ABI (Binary Interface application) to help control its actions.

The rules of pointers and lifetimes are expressed in terms of a "safely derived pointer" (3.7.4.3), roughly: "a pointer to something highlighted by a new one or under its object." to ordinary mortals: [...]

Functions in the C ++ standard that support this (the "interface" to which Stroustrup refers):

These features are presented in the N2670 offer :

Its goal is to support both garbage collection and leakage sensors based on reachability. This is done by providing undefined behavior for programs that β€œhide the pointer,” for example, with a different value, and then turn it back into a regular pointer and play it out. 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 leakage sensors may erroneously report such memory leakage programs.

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

I don't know any real standard C ++ GC implementation, but at least the standard sets the stage for this .

+8
source share

In addition to the good quantdev answer I supported, I would like to provide a little more information here (which would not fit the comment).

Here is the corresponding C ++ 11 program that demonstrates whether the implementation supports the GC interface:

 #include <iostream> #include <memory> int main() { #ifdef __STDCPP_STRICT_POINTER_SAFETY__ std::cout << __STDCPP_STRICT_POINTER_SAFETY__ << '\n'; #endif switch (std::get_pointer_safety()) { case std::pointer_safety::relaxed: std::cout << "relaxed\n"; break; case std::pointer_safety::preferred: std::cout << "preferred\n"; break; case std::pointer_safety::strict: std::cout << "strict\n"; break; } } 

Output:

 relaxed 

means the implementation has a trivial implementation that does nothing.

LibC ++ output:

 relaxed 

VS-2015 Outputs:

 relaxed 

gcc 5.0:

 prog.cc: In function 'int main()': prog.cc:10:13: error: 'get_pointer_safety' is not a member of 'std' switch (std::get_pointer_safety()) ^ 
+6
source share

All Articles