The careful handling of resources - may it be for example memory, files or sockets - is a key concern of programming in C++. This holds, in particular, true for the embedded programming that is often characterized by limited resources. Therefore, I will write a few posts about this challenging and versatile topic.
In my first attempt to write this post, I wanted to directly jump into the memory allocation with operator new. Fortunately, I went a few steps back. Memory management is only one but admittedly an important component of resource management in C++. The story is a lot more versatile. So I want to write about three domains of resource management in C++.
There is automatic memory management in C++ that is quite easy to use. I addition we have the well-known idioms in C++ that are the base of automatic memory management. At last, C++ offers explicit memory management in which the user has the full power at his disposal. I will follow these three steps in my post. Because I don't want to create the impression that explicit memory management is basic knowledge for the C++ developer.
Automatic memory management
I start at the basic level with smart pointers.
C++ has four different smart pointers. Each of them has the task to take care of the lifetime of it underlying resource. The std::unique_ptr takes explicitly ownership of the lifetime of one resource. A std::shared_ptr shares the ownership of a resource with the other std::shared_ptr's. Therefore, cycles can appear and the resource can not automatically be released. It's the job of the std::weak_ptr to break these cycles. The last one is the with C++11 deprecated std::auto_ptr. Why? You will see it in a later post.
STL container cont like std::vector or std::string automatically manage their memory. E.g.: they have a method cont.push_back to add a new element to the container. The size of the container will automatically grow. But it works also the other way around. Thanks to cont.shrink_to_fit, the container will be reduced to its size.
Each modern STL implementations uses the C++ idioms to move semantic, perfect forwarding, and the RAII idiom very often. To understand the underlying mechanism we have to dig deeper into the details of automatic memory management. Before I write about the C++ idioms here is a small appetizer.
The key idea of move semantic is quite simple: use cheap move operations instead of expensive copy operations for big objects. This holds, in particular, true for such objects that can not be copied. A typical example is a std::mutex or a std::unique_ptr.
Perfect forwarding uses under the hood similar techniques like the move semantic. The key idea of perfect forwarding is a different one. The job of perfect forwarding is to take arguments in a function template and forward them identically. The typical use case is constructors that forward their argument to the base class constructor or factory methods which creates new objects.
The idea of the RAII idiom is quite simple. You bind the acquisition and the release of a resource to the lifetime of a local object. Therefore, the resource will automatically be initialized in the constructor and released in the destructor. The acronym RAII stands for Resource Acquisition Is Initialization. Smart pointer and locks implement this technique.
Explicit memory management
Now we are in the domain of the experts. Thanks to the explicit memory management in C++ it is possible the tailor memory management to your needs. You can adjust it to simple objects or arrays, but you can also adjust it on a class level or global level. You can write your own memory allocators and use them with the help of placement new.
As promised I will write about the various flavours of memory management in C++ from top to bottom. I start in the next post with automatic memory management with smart pointers.
Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, Marko, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Animus24, Jozo Leko, John Breland, espkk, Wolfgang Gärtner, Louis St-Amour, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Neil Wang, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Tobi Heideman, Daniel Hufschläger, Red Trip, Alexander Schwarz, Tornike Porchxidze, Alessandro Pezzato, and Evangelos Denaxas.
Thanks in particular to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, and Richard Sargeant.
My special thanks to Embarcadero
I'm happy to give online-seminars or face-to-face seminars world-wide. Please call me if you have any questions.
Standard Seminars (English/German)
Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.
- Phone: +49 152 31965939
- German Seminar Page: www.ModernesCpp.de
- English Seminar Page: www.ModernesCpp.net