Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does C++ have a Garbage Collector?

I'm currently reading an unreleased master thesis report, that I'm going to give feedback on.

In the report they mention Garbage Collector under native C++ and managed C++. I thought C++ didn't have any standard GC, am I wrong or right? (They do not mention Boehm-Demers-Weiser.)

They have some problem getting it to work under some conditions. They create objects in one thread, and then delete the pointer from another thread.

like image 205
Marcus Johansson Avatar asked Feb 23 '11 09:02

Marcus Johansson


People also ask

Which language does not have garbage collection?

This is one of many reasons why languages like Java and C# are slower than C and C++ by design. And it is also the reason why C and C++ don't have and never will have a garbage collector, since those languages prioritize execution speed.

Why C has no garbage collection?

There are two reasons why C / C++ doesn't have garbage collection. It is "culturally inappropriate". The culture of these languages is to leave storage management to the programmer. It would be technically difficult (and expensive) to implement a precise garbage collector for C / C++.

Does C++ have garbage collection?

A C++ program can contain both manual memory management and garbage collection happening in the same program. According to the need, either the normal pointer or the specific garbage collector pointer can be used. Thus, to sum up, garbage collection is a method opposite to manual memory management.

How is garbage collection done in C?

Garbage Collection (GC) is a mechanism that provides automatic memory reclamation for unused memory blocks. Programmers dynamically allocate memory, but when a block is no longer needed, they do not have to return it to the system explicitly with a free() call.


2 Answers

Native C++ by default has no such thing (the closest thing to this are the smart pointers, but that's still something entirely different), but that doesn't prevent you from writing your own garbage collection solution (or using third party solution).

Managed C++ (and its successor C++/CLI) of course use .NET garbage collection for managed resources (though native resources are not garbage collected and have to be managed manually as in native C++).

like image 189
Matěj Zábský Avatar answered Oct 09 '22 14:10

Matěj Zábský


Existing C++ standard of 1998/2004 does not specify a garbage collector. The upcoming standard C++0x does specify an optional garbage collector API, however the implementation is an other part. With all that said, there are garbage collectors available for C++ from compiler vendors and third party.

  • GCC suite provides Boehm-GC for garbage collection.
  • Managed C++ was Microsoft's extension to C++ released with .Net 1.0 which extended C++ with garbage collection capabilities.
  • There is also C++/CIL from Microsoft released with .Net 2 which deprecated Managed C++ with more .Net centric features.
  • Sun Provided libgc as garbage collector for C/C++.
like image 13
Muhammad Anjum Kaiser Avatar answered Oct 09 '22 14:10

Muhammad Anjum Kaiser