2

I just started to learn C++ and was wondering if there's something like Java's GC.

Sorry if this question is too basic. The answers I found were too technical for me.

An explanation, in simple terms...

10
  • en.m.wikipedia.org/wiki/Boehm_garbage_collector perhaps? Commented Dec 30, 2022 at 20:31
  • 9
    Why do you think you need it? Commented Dec 30, 2022 at 20:34
  • 5
    Not a basic topic at all. Bolting new functionality on after the fact, and C++ wasn't designed with garbage collection in mind, is always tricky. Probably the most common point of failure in software. C++ follows a very different ideology where garbage collection isn't necessary. Read up on RAII. Commented Dec 30, 2022 at 20:41
  • 2
    One problem with garbage collection languages is you loose deterministic destruction and freeing of resources ie RAII bites the dust. Commented Dec 30, 2022 at 22:05
  • @user4581301 What's up with RAII? We're talking about memory allocation, RAII should work very well Commented Dec 31, 2022 at 3:28

3 Answers 3

4

Ok, here is my take on the subject. You could try Hans Boehm conservative GC and it should (well, famous last words) work just fine with C++.

When we allocate object via new in C++, first we call new operator to get raw memory, and then we execute new expression which does object initialization, calling constructors etc. Look here for the discussion, but in the nutshell, Boehm GC replaces the global ::new operator with calls to GC API.

Global ::delete operator is also replaced with what is basically empty call - GC will reclaim memory back. Delete expression will be the same and whole RAII machinery should just work.

It is well written and decent piece of software, and if I would need GC for C++ project, this is the place where I would start.

Sign up to request clarification or add additional context in comments.

Comments

3

C++ has no default GC. The stack variables are controlled by RAII. The unique_ptr and shared_ptr can used to automatically manage objects alocated on the heap. You can also optionally use a fully tracked pointer.

1 Comment

This is the correct answer. You don't need GC in C++. Instead you use RAII. Use constructors to allocate/open/obtain resources and destructors to free/close/release/clean up.
2

As long as you don't use new you should have garbage collection automatically.

Manage allocated memory by only using make_unique and make_shared for unique_ptr and shared_ptr respectively.

4 Comments

And what happens when you create a circular reference? Pretending that shared_ptr and other tools are exactly equivalent to garbage collection is wrong.
This is nonsense. If you could get malloc from GC lib, you could overload operator new as well using the same GC
@NicolBolas use a weak_ptr.
@Surt: Solving circular references isn't the problem. Discovering you have a circular reference is the problem. With proper GC, the system automatically handles it. The fact that you have to solve it yourself, you have to know when a circular reference happens and break it, means that it's not a GC solution. These are useful tools, but they're hardly automatic memory management.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.