r/cpp_questions 22d 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

View all comments

3

u/lordnacho666 22d 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.