Will C++ have a garbage collector someday?
Maybe - here is a book that discusses this subject: http://accu.informika.ru/accu/bookreviews/public/reviews/g/g000560.htm
there is also an interesting article at ACCU on this which discusses the issue for various languages: http://accu.org/index.php/journals/244
the latest ACCU edition also has a related article on this called "RAII is not Garbage": http://accu.org/var/uploads/journals/overload106.pdf
But garbage collector is not very good thing because if program is doing something important maybe in medicine and during surgery runs out of memory and then everything freezes and everyone needs to wait for garbage collector to make more space. That's why important programs is written in c++ not in java
Wouldn't the same problem occur even in a C++ application?
you can control memory yourself in c++ but you cant in java
so you can't delete objects yourself in java?
yeah
so it's entirely up to the garbage collector.... write a better garbage collector then!
one that frees stuff as soon as possible.
garbage collector is called only when there's no memory left and then he looks which objects are not used and remove it and try to rearrange memory
i think it wouldn't be efficient to rearrange data every time
modern garbage collectors have improved a lot - so you don't get the old-style "freezing" of your application anymore with these types of "incremental" garbage collectors. but @Tomas.A is right is stating that C++ gives you much more control over when and how you want to free up your memory.
oh well.... garbage collectors are often written in C and C++ anyway :-P
see here for How Garbage Collection Works in Java: http://javarevisited.blogspot.com/2011/04/garbage-collection-in-java.html
there is also a recent article on InfoQ here: http://www.infoq.com/presentations/Understanding-Java-Garbage-Collection
yeah my lecturer mentioned that it's possible to have incremental garbage collector but didn't comment anything about it
GC is hard, and there's no efficient way to tell the instant something becomes garbage. That's why you can't just free something as soon as the user is done with it. That said, there are already a couple of garbage collectors out there for C++, including the Boehm garbage collector: http://www.hpl.hp.com/personal/Hans_Boehm/gc/
Join our real-time social learning platform and learn together with your friends!