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...

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • https://en.m.wikipedia.org/wiki/Boehm_garbage_collector perhaps? – Severin Pappadeux Dec 30 '22 at 20:31
  • 9
    Why do you think you need it? – Captain Obvlious Dec 30 '22 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](https://en.cppreference.com/w/cpp/language/raii). – user4581301 Dec 30 '22 at 20:41
  • 2
    One problem with garbage collection languages is you loose deterministic destruction and freeing of resources ie RAII bites the dust. – Richard Critten Dec 30 '22 at 22:05
  • @user4581301 What's up with RAII? We're talking about memory allocation, RAII should work very well – Severin Pappadeux Dec 31 '22 at 03:28
  • @RichardCritten No, you don't. RAII works fine. What you're loosing is deterministic memory freeing, but I don't believe standard conforming code should rely on deallocated memory. – Severin Pappadeux Dec 31 '22 at 03:30
  • @SeverinPappadeux RAII is fine with automatic variables, but what if one wants RAII mechanism with dynamic variable, i.e. `std::shared_ptr` will to work as expected with GC? – sklott Dec 31 '22 at 05:04
  • @sklott yes. Boehm GC replaces ::new operator and ::delete operator. New delete expressions still there. Unless there is some nonportable magic in the implementation, it should work. Please check my answer for details – Severin Pappadeux Dec 31 '22 at 05:10
  • Follows a very different ideology may have been a poor choice of wording. C++ *promotes* a very different ideology is a better description. RAII is not at odds with garbage collection, you could employ both and I'm sure there are cases where both would be attractive. But before anyone goes that way, I strongly recommend reading [Raymond Chen's piece on the ideology behind garbage collection](https://devblogs.microsoft.com/oldnewthing/20100809-00/?p=13203) and think carefully on the cognitive cost of having to book-keep with two simultaneous resource management ideologies in play. – user4581301 Dec 31 '22 at 08:12
  • @user4581301 'C++ promotes a very different ideology' that I might agree with. For whole C++ machinery to work you HAVE TO HAVE new expression and matched delete expression. What is actually saved would be delete operator call, which would be empty function - GC would take over to reclaim memory. – Severin Pappadeux Dec 31 '22 at 21:56

3 Answers3

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.

traveh
  • 2,700
  • 3
  • 27
  • 44
Severin Pappadeux
  • 18,636
  • 3
  • 38
  • 64
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.

Pebal
  • 41
  • 1
  • 1
  • 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. – Dúthomhas Dec 31 '22 at 18:12
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.

Surt
  • 15,501
  • 3
  • 23
  • 39
  • 3
    And what happens when you create a circular reference? Pretending that `shared_ptr` and other tools are exactly equivalent to garbage collection is wrong. – Nicol Bolas Dec 30 '22 at 20:46
  • This is nonsense. If you could get malloc from GC lib, you could overload operator new as well using the same GC – Severin Pappadeux Dec 31 '22 at 02:56
  • @NicolBolas use a weak_ptr. – Surt Jan 01 '23 at 10:21
  • @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. – Nicol Bolas Jan 01 '23 at 14:22