Why doesn't C++ have a garbage collector?

I'm not asking this inquiry as a result of the qualities of garbage collection first off. My major factor for asking this is that I do recognize that Bjarne Stroustrup has actually claimed that C+npls will certainly have a garbage man at some time in time.

With that said claimed, why hasn't it been included? There are currently some garbage man for C+npls. Is this simply among those "easier claimed than done" type points? Or exist various other factors it hasn't been included (and also will not be included C+npls 11)?

Cross links:

Just to make clear, I recognize the reasons that C+npls really did not have a garbage man when it was first developed. I'm asking yourself why the enthusiast can not be included.

2022-07-25 16:28:22
Source Share
Answers: 6

One of the biggest reasons that C++ doesn't have built in garbage collection is that getting garbage collection to play nice with destructors is really, really hard. As far as I know, nobody really knows how to solve it completely yet. There are alot of issues to deal with:

  • deterministic lifetimes of objects (reference counting gives you this, but GC doesn't. Although it may not be that big of a deal).
  • what happens if a destructor throws when the object is being garbage collected? Most languages ignore this exception, since theres really no catch block to be able to transport it to, but this is probably not an acceptable solution for C++.
  • How to enable/disable it? Naturally it'd probably be a compile time decision but code that is written for GC vs code that is written for NOT GC is going to be very different and probably incompatible. How do you reconcile this?

These are just a few of the problems faced.

2022-07-25 18:40:34

The idea behind C++ was that you would not pay any performance impact for features that you don't use. So adding garbage collection would have meant having some programs run straight on the hardware the way C does and some within some sort of runtime virtual machine.

Nothing prevents you from using some form of smart pointers that are bound to some third-party garbage collection mechanism. I seem to recall Microsoft doing something like that with COM and it didn't go to well.

2022-07-25 17:56:44

What type? should it be optimised for embedded washing machine controllers, cell phones, workstations or supercomputers?
Should it prioritise gui responsiveness or server loading?
should it use lots of memory or lots of CPU?

C/c++ is used in just too many different circumstances. I suspect something like boost smart pointers will be enough for most users

Edit - Automatic garbage collectors aren't so much a problem of performance (you can always buy more server) it's a question of predicatable performance.
Not knowing when the GC is going to kick in is like employing a narcoleptic airline pilot, most of the time they are great - but when you really need responsiveness!

2022-07-25 17:55:02

To answer most "why" questions about C++, read

2022-07-25 17:54:32

If you desire automated garbage collection, there are excellent commercial and also public - domain name garbage man for C+npls. For applications where garbage collection appropriates, C+npls is a superb waste accumulated language with an efficiency that contrasts positively with various other waste accumulated languages. See for a conversation of automated garbage collection in C+npls. See additionally, Hans - J. Boehm is ().

Additionally, C+npls sustains shows strategies that permit memory monitoring to be . I take into consideration garbage collection a last selection and also an incomplete means of taking care of for source monitoring. That does not suggest that it is never ever valuable, simply that there are far better strategies in several scenarios.


As for why it does not have it constructed in, If I bear in mind appropriately it was designed prior to GC was the point , and also I do not think the language can have had GC for numerous factors (I.E Backwards compatibilty with C)

Hope this aids.

2022-07-25 17:54:25

Implicit garbage collection can have been included, yet it simply really did not make it. Possibly as a result of not simply execution difficulties, yet additionally as a result of individuals not having the ability to involve a basic agreement quickly sufficient.

A quote from Bjarne Stroustrup himself:

I had actually wished that a garbage man which can be additionally made it possible for would certainly become part of C+npls 0x, yet there sufficed technological troubles that I need to use simply a thorough requirements of just how such an enthusiast incorporates with the remainder of the language, if given. As holds true with basically all C+npls 0x attributes, a speculative execution exists.

There is an excellent conversation of the subject .

General overview:

C+npls is really effective and also permits you to do virtually anything. Consequently it does not instantly push several points onto you that could influence efficiency. Garbage collection can be conveniently applied with clever reminders (things that cover reminders with a reference matter, which vehicle delete themselves when the reference matter gets to 0).

C+npls was constructed with rivals in mind that did not have garbage collection. Performance was the major problem that C+npls needed to repel objection from in contrast to C and also others.

There are 2 sorts of garbage collection

Explicit waste collection:

C+npls 0x has garbage collection using reminders developed with shared_ptr

If you desire it you can utilize it, if you do not desire it you aren't pushed into utilizing it.

For variations prior to C+npls 0x, increase: shared_ptr exists and also offers the very same objective.

Implied waste collection:

It does not have clear garbage collection though. It will certainly be an emphasis factor for future C+npls specifications though.

Why Tr1 does not have implied garbage collection?

There are a great deal of points that tr1 of C+npls 0x needs to have had, Bjarne Stroustrup in previous meetings mentioned that tr1 really did not have as high as he would certainly have suched as.

2022-07-25 17:53:30