C++ – In C++, how much programmer time is spent doing memory management

cmemory

People who are used to garbage collected languages are often scared of C++'s memory management. There are tools, like auto_ptr and shared_ptr which will handle many of the memory management tasks for you. Lots of C++ libraries predate those tools, and have their own way to handle the memory management tasks.

How much time do you spend on memory management tasks?

I suspect that it is highly dependent on the set of libraries you use, so please say which ones your answer applies to, and if they make it better or worse.

Best Answer

Modern C++ makes you not worry about memory management until you have to, that is until you need to organize your memory by hand, mostly for optimization purpose, or if the context forces you to do it (think big-constraints hardware). I've written whole games without manipulating raw memory, only worriing about using containers that are the right tool for the job, like in any language.

So it depends on the project but most of the time it's not memory management that you have to handle but only object life-time. That is solved using smart pointers, that is one of idiomatic C++ tool resulting from RAII.

Once you understand RAII, memory management will not be a problem.

Then when you'll need to access raw memory, you'll do it in very specific, localized and identifiable code, like in pool object implementations, not "everywhere".

Outside of this kind of code, you'll not need to manipulate memory, only objects lifetime.

The "hard" part is to understand RAII.

Related Topic