{"id":5056,"date":"2016-12-05T06:01:32","date_gmt":"2016-12-05T06:01:32","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/careful-handling-of-resources\/"},"modified":"2023-06-26T12:34:01","modified_gmt":"2023-06-26T12:34:01","slug":"careful-handling-of-resources","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/careful-handling-of-resources\/","title":{"rendered":"Careful Handling of Resources"},"content":{"rendered":"<p>The careful handling of resources &#8211; may it be, for example, memory, files, or sockets &#8211; is a key concern of programming in C++. This holds, in particular, true for embedded programming, which is often characterized by limited resources. Therefore, I will write a few posts about this challenging and versatile topic.<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<\/p>\n<p>In my first attempt to write this post, I wanted to jump directly 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++.<\/p>\n<p>There is automatic memory management in C++ that is relatively easy to use. In 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 full power. I will follow these three steps in my post because I don&#8217;t want to create the impression that explicit memory management is basic knowledge for the C++ developer.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5055\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/12\/RessourcenmanagementEng.png\" alt=\"RessourcenmanagementEng\" width=\"700\" height=\"675\" style=\"margin: 15px;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/12\/RessourcenmanagementEng.png 745w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/12\/RessourcenmanagementEng-300x289.png 300w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><\/p>\n<h2>Automatic memory management<\/h2>\n<p>I start at the basic level with smart pointers.<\/p>\n<h3>Smart pointer<\/h3>\n<p>C++ has four different smart pointers. Each of them has the task of taking care of the lifetime of its underlying resource. The <span style=\"font-family: courier new,courier;\">std::unique_ptr<\/span> takes ownership of the lifetime of one resource explicitly. A <span style=\"font-family: courier new,courier;\">std::shared_ptr<\/span> shares the ownership of a resource with the other <span style=\"font-family: courier new,courier;\">std::shared_ptr<\/span>&#8216;s. Therefore, cycles can appear and the resource can not automatically be released. It&#8217;s the job of the&nbsp;<span style=\"font-family: courier new,courier;\">std::weak_ptr<\/span> to break these cycles. The last one is the with C++11 deprecated <span style=\"font-family: courier new,courier;\">std::auto_ptr.<\/span> Why? You will see it in a later post.<\/p>\n<h3>STL container<\/h3>\n<p>STL container <span style=\"font-family: courier new,courier;\">cont<\/span> like<span style=\"font-family: courier new,courier;\"> <\/span><span style=\"font-family: courier new,courier;\">std::vector<\/span> or <span style=\"font-family: courier new,courier;\">std::string<\/span> automatically manages their memory. E.g., they have a method<span style=\"font-family: courier new,courier;\"> cont.push_back <\/span>to add a new element to the container.&nbsp;The size of the container will automatically grow. But it also works the other way around. Thanks to <span style=\"font-family: courier new,courier;\">cont.shrink_to_fit,<\/span> the container will be reduced to its&nbsp;size.<\/p>\n<\/p>\n<h2>C++ idioms<\/h2>\n<p>Each modern STL implementation uses the C++ idioms to move semantic, perfect forwarding, and the RAII idiom very often. To understand the underlying mechanism, we must dig deeper into automatic memory management details. Before I write about the C++ idioms, here is a small appetizer.<\/p>\n<h3>Move semantic<\/h3>\n<p>The critical idea of move semantics is 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 <span style=\"font-family: courier new,courier;\">std::mutex<\/span> or a <span style=\"font-family: courier new,courier;\">std::unique_ptr.<\/span><\/p>\n<h3>Perfect forwarding<\/h3>\n<p>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 that create new objects.<\/p>\n<h3>RAII idiom<\/h3>\n<p>The idea of the RAII idiom is quite simple. You bind a resource&#8217;s acquisition and release to the lifetime of a local object. Therefore, the resource will automatically be&nbsp;initialized in the constructor and released in the destructor. The acronym RAII stands for <strong>R<\/strong>esource <strong>A<\/strong>cquisition <strong>I<\/strong>s <strong>I<\/strong>nitialization. Smart pointers and locks implement this technique.<\/p>\n<h2>Explicit memory management<\/h2>\n<p>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 or global level. You can write your memory allocators and use them with the help of <span style=\"font-family: courier new,courier;\">placement new. <\/span><\/p>\n<h2>What&#8217;s next?<\/h2>\n<p>As promised, I will&nbsp;write about the various flavors of memory management in C++ from top to bottom. I start in the <a href=\"https:\/\/www.modernescpp.com\/index.php\/memory-and-performance-overhead-of-smart-pointer\">next post<\/a> with automatic memory management with smart pointers.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The careful handling of resources &#8211; may it be, for example, memory, files, or sockets &#8211; is a key concern of programming in C++. This holds, in particular, true for embedded programming, which is often characterized by limited resources. Therefore, I will write a few posts about this challenging and versatile topic.<\/p>\n","protected":false},"author":21,"featured_media":5055,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[364],"tags":[498,399],"class_list":["post-5056","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-embedded","tag-memory","tag-smart-pointers"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5056","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=5056"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5056\/revisions"}],"predecessor-version":[{"id":6918,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5056\/revisions\/6918"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/5055"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=5056"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=5056"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=5056"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}