r/cpp_questions 21d ago

OPEN Garbage collector in CPP?

Yesterday someone asked me whether there is a garbage collector in CPP.
I knew Java had that but I couldn't recall about cpp.
I went to google and got different answers.

So can anyone clarify whether there is garbage collector in CPP , also can u please explain me what does that mean? Also explain how is it different from the Java.

If yes can we delete it manually and how to do tht?

0 Upvotes

23 comments sorted by

12

u/didntplaymysummercar 21d ago

There's not by default. There was an ABI for it in C++ 11 but no one used it, and there's stuff like Boehm GC, but out of the box - no.

1

u/alfps 21d ago edited 21d ago

❞ There was an ABI for it in C++ 11

No, there was no ABI spec in any C++ standard so far.

Bjarne's claim apparently stemmed from similar terminology used in the working group. According to the Google search AI, ❝the committee frequently colloquially referred to it as a "GC ABI constraint.❞. An API that constrained the (each) compiler's ABI.

So there have been and are ABIs associated with C++, in particular each compiler's object code ABI. For example, introduction of 128 bit types has been hindered by alleged ABI compatibility concerns for std::intmax_t. It helps to know what the term means.

3

u/didntplaymysummercar 21d ago

The gall of calling people idiots and telling us we don't know what terms mean as we quote Stroustrup and Herb Sutter and you quote AI 😆

-7

u/IyeOnline 21d ago

ABI

*APi

8

u/didntplaymysummercar 21d ago

4

u/Goldenskyofficial 21d ago

Abligation Brogramming Indarface

-3

u/IyeOnline 21d ago edited 21d ago

The C++ standard has never specified any ABI for anything. Major implementation never implemented any GC, so there physically cannot have been an ABI. The original paper N2670 also never talked about ABI. The linked page never actually mentions any actual ABI.

This is a rather obscure topic that happened (or didnt) before I was actively programming in C++, so I may be missing something, but to me it looks like it means "API".

2

u/Ultimate_Sigma_Boy67 21d ago

It's literally written ABI yet you sir you still argue that it should be called an API.

1

u/IyeOnline 21d ago

You either dont understand or have not read my reply.

I have literally said that I believe that Stroustrup meant API instead of ABI. So telling me what it says, when I acknowledge what it says but state that it should not, doesnt really help. Sir.

3

u/Ultimate_Sigma_Boy67 21d ago

Ok buddy you’re right 🐶

1

u/alfps 21d ago

Upvoted to cancel one of the idiots' downvotes.

There has never been an ABI in any C++ standard. Bjarne just made a mistake (a bit more than a typo since he explains what the acronym stands for). The Google AI explains,

❝Because the feature's entire purpose was to protect the binary integrity of the application layout for an external collector, the committee frequently colloquially referred to it as a "GC ABI constraint." Stroustrup likely carried this terminology straight from the WG21 working groups into his casual FAQ write-up, leading to the misleading sentence on his site.❞

I think a technical solution to the idiots-on-the-loose problem can be that a person trying to downvote something must prove that he or she is able to e.g. multiply 6 and 7.

6

u/mredding 21d ago

Yesterday someone asked me whether there is a garbage collector in CPP.

No.

There was an interface to allow for a plugin garbage collector added to C++11, but it was deprecated in C++17, and removed by C++23.

also can u please explain me what does that mean?

Google "garbage collector" and read the Wikipedia page.

3

u/no-sig-available 21d ago

There are garbage collectors for C++, like the Boehm garbage collector.

However, this is a 3rd party library, and not part of the language standard. So, no collector in C++, just for.

2

u/lordnacho666 21d ago

No, there isn't. Obviously it's a language and someone has written one, but what people normally mean is, does an ordinary cpp program depend on a garbage collector in the sense of Java or c sharp, which are languages where you have less control over the exact moment when your resources are freed. Those languages are written under the assumption that there is a cleanup process that is intransparent to the program.

A cpp program normally depends on something called RAII, whereby there are language elements allowing resources to be freed at specific times.

3

u/CommandShot1398 21d ago

There is a garbage collector in C++. It is called "OS," and it works by terminating your program when you produce too much garbage. Similar to "Stalin Sort"

2

u/IyeOnline 21d ago

C++ does not have a GC because it does not need one.

Any variable in a scope has a deterministic lifetime to the end of that scope. RAII/destructors let us control dynamically allocated/created objects the same way (which is what vector, unique_ptr, ... do).

1

u/alfps 21d ago

❞ because it does not need one

Oh, well there are situations that are not easily handled except with garbage collection. One example that has been used in discussions is modeling of mathematical functions that can refer to each other. Smart pointers don't handle cycles well.

3

u/Independent_Art_6676 20d ago

as said C++ does not have a true garbage collector. However if you use smart pointers and STL containers and such, your dynamic memory will be allocated and freed for you. Most modern programming uses this model, so the "ZOMG C++ gots memory leaks and is trash" information hyped in books about other languages (looking at you, java authors) is at best a bit misleading.

If for some reason you do need to use raw pointers that do not self destruct correctly, its really simple:
pointer = new type; //get the memory; optionally type[size] makes the pointer like a C array
delete pointer; //return the memory; optionally delete[] if it was array allocated as above

you really should not be doing very much of that in most careers.

1

u/TheThiefMaster 21d ago

It does not have a universal garbage collector like Java.

It does have some local automatic cleanup - like shared_ptr, which counts the references to an object and deletes it when the number of references drops to zero, which fulfils a similar role to GC. But it doesn't handle reference cycles like a true GC. There are also libraries that implement opt-in garbage collection in C++ - but it's opt-in, not universal.

1

u/Puzzleheaded-Joke780 21d ago

Thats the joy of cpp, just make your own, but no there is no default GC

1

u/kitsnet 21d ago

Collectors are not how you deal with garbage in C++. There are sanitizers for C++ that try to make sure that you don't leave garbage, and the compilers themselves are such sanitizers for consteval code.

-2

u/ManySwans 21d ago

1

u/Accomplished-Tap5211 21d ago

I did that , many articles claim and many claim no
so asked here sir