43

In C++11's language feature list there is:

Minimal support for garbage collection and reachability-based leak detection

(but it seems not implemented in either GCC and Clang.)

Why the standard committee introduced this garbage collection C++ langauge feature?

Does C++ really need a GC? Isn't RAII such an excellent pattern (that can be used uniformly for both memory and non-memory resources, like sockets, files, textures...)?

Will a GC break the uniformity of C++ code pattern that uses RAII?

Some people say that a GC can come in handy to break circular dependencies, but isn't it just OK to use smart pointers like weak_ptr for this purpose?

And what happens in case of exceptions thrown? How will the stack unwind semantics be modified to take a GC into consideration?

And will a C#-like IDisposable pattern be introduced as well?

Moreover, assuming that a GC is introduced in C++, will the pointer syntax be different? e.g. Will we have some hat-like "pointers" ^ like in C++/CLI or C++/CX extensions? There should be a way to differentiate from ordinary raw pointers vs. "managed" pointers, right?

Mr.C64
  • 41,637
  • 14
  • 86
  • 162
  • 8
    C++ does not have a GC and it will never have one. – Alex Chamberlain Mar 01 '13 at 12:19
  • `weak_ptr` is static, it cannot break dynamic cycles. And as that says, it adds *minimal support* (it basically defines stuff related to reachability). All the rest is pure speculation, and does not belong on SO. – R. Martinho Fernandes Mar 01 '13 at 12:19
  • 27
    @AlexChamberlain GCs for C++ have been around for ages now. – R. Martinho Fernandes Mar 01 '13 at 12:19
  • 2
    Garbage collection is not part of the language. However, the new language features attempt to make it easier to create garbage-collection *libraries* for C++. – Kerrek SB Mar 01 '13 at 12:21
  • @R.MartinhoFernandes: I would not call it speculation. The question about what happens when a C++ exception is thrown, or what syntax the GC pointer requires are very concrete. – Mr.C64 Mar 01 '13 at 12:21
  • @R.MartinhoFernandes Really!?! Reference counted patterns have been around for ages now; these are not GCs! – Alex Chamberlain Mar 01 '13 at 12:21
  • 2
    @Mr.C64 it isn't. There is no such specification anywhere: any implementation is free to do whatever they want. – R. Martinho Fernandes Mar 01 '13 at 12:21
  • 1
    @Alex here's the most well-known one: http://www.hpl.hp.com/personal/Hans_Boehm/gc/ – R. Martinho Fernandes Mar 01 '13 at 12:22
  • @R.MartinhoFernandes: So it this C++11 GC something non-portable and under-specified? – Mr.C64 Mar 01 '13 at 12:22
  • 1
    It's *minimal support*: it basically gives implementations the leeway (and minimal definitions and guarantees) to provide such a thing if they want. Nothing else. – R. Martinho Fernandes Mar 01 '13 at 12:23
  • 5
    @AlexChamberlain C++ already has GC, it's just not standard. In fact, there was a proposal to add it to C++11, but the committee just ran out of time. – James Kanze Mar 01 '13 at 12:58
  • @Mr.C64: the properties of any GC that the implementation may provide are as "non-portable and under-specified" as the properties of any sockets interface that the implementation may provide. So yes, it's not specified by the standard. The difference in C++11 is that the standard permits garbage collectors to free stuff under certain well-defined conditions. Previously this wasn't permitted, and actual garbage-collecting implementations would fail for programs with sftrabbit's "funny business". In C++11, strict pointer safety means "funny business is UB". – Steve Jessop Mar 01 '13 at 13:17
  • Already had a similar question: http://stackoverflow.com/questions/14062856/why-does-c11-allow-for-gc/14067109 – Ivan Mar 01 '13 at 20:43

3 Answers3

60

The proposal doesn't introduce a garbage collector - it just allows for it in certain situations if the implementation chooses. The standard will just describe these situations as causing undefined behaviour. In doing this, it relaxes the requirements of the implementation, giving the minimal leeway for a garbage collector.

The simple example given in the proposal considers when you take a pointer to a dynamically allocated object, XOR it with another value, thereby hiding the pointer value, and then recover the original pointer value to access the object through it. Before C++11, this would be perfectly fine and it would still be valid to use. However, now such an operation may be (see next paragraph) considered undefined behaviour, which means that an implementation may do garbage collection on the object that was pointed to.

The standard states that an implementation can either have relaxed pointer safety, in which case the behaviour is as it was before, or strict pointer safety, which allows for the introduction of a garbage collector.

An implementation may have relaxed pointer safety, in which case the validity of a pointer value does not depend on whether it is a safely-derived pointer value. Alternatively, an implementation may have strict pointer safety, in which case a pointer value that is not a safely-derived pointer value is an invalid pointer value unless the referenced complete object is of dynamic storage duration and has previously been declared reachable (20.6.4). [...] It is implementation defined whether an implementation has relaxed or strict pointer safety.

A pointer value is a safely-derived pointer value if it points at a dynamically allocated object and hasn't had any funny business happen to it (defined more specifically in §3.7.4.3).

If your implementation has strict pointer safety yet you still want to do said funny business to a pointer without introducing undefined behaviour, you can declare a pointer p as being reachable like so:

declare_reachable(p);

This function is defined in the <memory> header, along with related functions such as undeclare_reachable, declare_no_pointers, and undeclare_no_pointers. You can also determine the strictness of your implementation using get_pointer_safety.

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
  • So for example there is no special syntax specified for the managed objects? No ^ or something? And the standard doesn't say anything about the behavior of the GC if an exception is thrown? – Mr.C64 Mar 01 '13 at 12:25
  • @Mr.C64 Exactly. There is no mention of garbage collector. – Joseph Mansfield Mar 01 '13 at 12:25
21

From Bjarne Stroustrup:

Actually, what I said was something like "When (not if) automatic garbage collection becomes part of C++, it will be optional".

http://www.stroustrup.com/slashdot_interview.html

fredoverflow
  • 256,549
  • 94
  • 388
  • 662
fjardon
  • 7,921
  • 22
  • 31
2

There is a proposal to remove this GC support from standard:

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p2186r0.html

and there is an C++ GC(Boehm): https://www.hboehm.info/gc/

Baiyan Huang
  • 6,463
  • 8
  • 45
  • 72