C++ Garbage Collection

January 28, 2014

C++ is not a perfect language. There isn't one. It has idiosyncrasies and gotcha usage that must be understood to make efficient and effective use of it. It's insanely flexible and as high level or low level as you want and this contributes to making it a difficult language to advance with- in part because its goal is not to limit what the programmer can do or needs to know.

What this means is programming in C++ is a real world example of grown men and women touching a hot stove and yelling ouch. Then, touching the same hot stove again only moments later. And again. At some point they'll start complaining that their hand hurts and that the stove is hot. Then, they'll touch it again.

After weeks, months, or maybe years they'll start looking for something else to touch. They simply can't stand by the stove and not burn their hand.

That is how many career C++ programmers write code and I only need to pick one simple C++ concept (of many) to help fix it. C++ allows a programmer to explicitly acquire a resource such as memory and at some later point, explicitly release that resource. It's a really simple concept. Get something, release something. Every C++ programmer I have ever known (including myself) has, at some point in their programming career, been compelled to do this and has had a problem with not doing this correctly. As systems get more complex and code bases get larger, this trivial handshake gets really complicated. The machine demands perfection. The result is memory leaks, poor performance, crashes and ultimately hours of debugging leading to failed products.

When this happens it's because of flaws and weaknesses with the C++ language that would just go away if they had a better, upgraded language with a slick IDE, or so they claim. They'll say, C++ is broken and they can't create great software with it. It eventually develops into being envious of the C# and the Java programmers who are sitting over on the other side of the room laughing and appear to be creating software easier.

They think they need a language that has garbage collection.

See, the problem is that this type of disgruntled C++ programmer thinks that using a different language will make them better programmers- just like all the Russian novelists complain they could write better novels if they could use English. That makes no sense.

This madness must stop.

There is a fundamental problem with how these C++ programmers develop software. They are stuck in old habits, mostly from C. They must stop using new and delete. That's right- they shouldn't be using it at all. Not using new and delete explicitly will fix the memory leak problem, make code simpler, and make code cleaner. It will also fix numerous other gotcha problems like exception safety. The end result is more correct code easier and written faster.
Don't use explicit new, delete, and owning * pointers, except in rare cases encapsulated inside the implementation of a low-level data structure. - Herb Sutter

Were you would otherwise be using a new, consider using std::auto_ptr, std::vector, or any of the many available smart pointers in boost or C++11. There's a simple little C++ idiom behind this called Resource Acquisition Is Initialization (RAII) that should be the cornerstone of every C++ programmer's mentality. If you want to know what the secret sauce is to being a better C++ programmer, it starts with this idiom. The best example implementation of RAII is smart pointers. I know programmers that have seen smart pointers and may have even fooled around with using them, but never really had the light-bulb moment on how to apply them in all aspects of writing C++ code. Maybe they never get past the extra syntax, not realizing it actually results in less code. Don't do that. A little discipline can go an incredibly long way.

You can get rid of every need for garbage collection by using RAII and you don't have to give up anything you would otherwise be trading off like non deterministic behavior or slower execution. Herb Sutter gave an excellent talk on how modern C++ code should be developed and that includes taking full advantage of RAII.

It's time to be a better programmer. It can all start with fact that the idea of RAII is newer and better than the idea of garbage collection in the modern sense. Lets not pretend that garbage collection didn't start out as an example of a reference counting smart pointer.

Related Posts

1 Comment

Comment February 3, 2014 by Dzutte
Aside of the technical facets touched in the article, can you name any of those Russian novelists complaining for the inability to use English? Have never heard of even a single one.