{"id":5749,"date":"2019-07-24T19:29:58","date_gmt":"2019-07-24T19:29:58","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-when-raii-breaks\/"},"modified":"2023-06-26T10:04:31","modified_gmt":"2023-06-26T10:04:31","slug":"c-core-guidelines-when-raii-breaks","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-when-raii-breaks\/","title":{"rendered":"C++ Core Guidelines: When RAII breaks"},"content":{"rendered":"<p>Before I write about the very popular RAII idiom in C++, I want to present you with a trick, which is often quite handy, when you repeatedly search for a text pattern:&nbsp; use negative search.<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5746\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/sports-1777806_1280.jpg\" alt=\"sports 1777806 1280\" width=\"600\" height=\"400\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/sports-1777806_1280.jpg 1280w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/sports-1777806_1280-300x200.jpg 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/sports-1777806_1280-1024x682.jpg 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/sports-1777806_1280-768x512.jpg 768w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<p>Often, the text patterns or tokens, you are looking for, are following a repetitive structure. Here, the negative search comes into play.<\/p>\n<h2>Use Negative Search if applicable<\/h2>\n<p>The general idea is easy to explain. You define a complicated regular expression to search for tokens. The tokens are often separated by delimiters such as colons, commas, spaces, and so on. In this case, it is easier to search for the delimiters, and the tokens you are interested in are just the text between the delimiters. Let&#8217;s see what I mean.<\/p>\n<p>&nbsp;<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ regexTokenIterator.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;string&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;regex&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;vector&gt;<\/span>\r\n\r\nstd<span style=\"color: #555555;\">::<\/span>vector<span style=\"color: #555555;\">&lt;<\/span>std<span style=\"color: #555555;\">::<\/span>string<span style=\"color: #555555;\">&gt;<\/span> splitAt(<span style=\"color: #006699; font-weight: bold;\">const<\/span> std<span style=\"color: #555555;\">::<\/span>string <span style=\"color: #555555;\">&amp;<\/span>text,                     <span style=\"color: #0099ff; font-style: italic;\">\/\/ (6)<\/span>\r\n                                 <span style=\"color: #006699; font-weight: bold;\">const<\/span> std<span style=\"color: #555555;\">::<\/span>regex <span style=\"color: #555555;\">&amp;<\/span>reg) {\r\n  std<span style=\"color: #555555;\">::<\/span>sregex_token_iterator hitIt(text.begin(), text.end(), reg, <span style=\"color: #555555;\">-<\/span><span style=\"color: #ff6600;\">1<\/span>);\r\n  <span style=\"color: #006699; font-weight: bold;\">const<\/span> std<span style=\"color: #555555;\">::<\/span>sregex_token_iterator hitEnd;\r\n  std<span style=\"color: #555555;\">::<\/span>vector<span style=\"color: #555555;\">&lt;<\/span>std<span style=\"color: #555555;\">::<\/span>string<span style=\"color: #555555;\">&gt;<\/span> resVec;\r\n  <span style=\"color: #006699; font-weight: bold;\">for<\/span> (; hitIt <span style=\"color: #555555;\">!=<\/span> hitEnd; <span style=\"color: #555555;\">++<\/span>hitIt)\r\n    resVec.push_back(hitIt<span style=\"color: #555555;\">-&gt;<\/span>str());\r\n  <span style=\"color: #006699; font-weight: bold;\">return<\/span> resVec;\r\n}\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> main() {\r\n\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n\r\n  <span style=\"color: #006699; font-weight: bold;\">const<\/span> std<span style=\"color: #555555;\">::<\/span>string text(<span style=\"color: #cc3300;\">\"3,-1000,4.5,-10.5,5e10,2e-5\"<\/span>);                      <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n\r\n  <span style=\"color: #006699; font-weight: bold;\">const<\/span> std<span style=\"color: #555555;\">::<\/span>regex regNumber(\r\n      R<span style=\"color: #cc3300;\">\"([-+]?([0-9]+\\.?[0-9]*|\\.[0-9]+)([eE][-+]?[0-9]+)?)\"<\/span>);                <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n  std<span style=\"color: #555555;\">::<\/span>sregex_iterator numberIt(text.begin(), text.end(), regNumber);         <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\r\n  <span style=\"color: #006699; font-weight: bold;\">const<\/span> std<span style=\"color: #555555;\">::<\/span>sregex_iterator numberEnd;\r\n  <span style=\"color: #006699; font-weight: bold;\">for<\/span> (; numberIt <span style=\"color: #555555;\">!=<\/span> numberEnd; <span style=\"color: #555555;\">++<\/span>numberIt) {                        \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> numberIt<span style=\"color: #555555;\">-&gt;<\/span>str() <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;                                <span style=\"color: #0099ff; font-style: italic;\">\/\/ (4)<\/span>\r\n  }\r\n\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n\r\n  <span style=\"color: #006699; font-weight: bold;\">const<\/span> std<span style=\"color: #555555;\">::<\/span>regex regComma(<span style=\"color: #cc3300;\">\",\"<\/span>);\r\n  std<span style=\"color: #555555;\">::<\/span>sregex_token_iterator commaIt(text.begin(), text.end(), regComma, <span style=\"color: #555555;\">-<\/span><span style=\"color: #ff6600;\">1<\/span>); <span style=\"color: #0099ff; font-style: italic;\">\/\/ (5)<\/span>\r\n  <span style=\"color: #006699; font-weight: bold;\">const<\/span> std<span style=\"color: #555555;\">::<\/span>sregex_token_iterator commaEnd;\r\n  <span style=\"color: #006699; font-weight: bold;\">for<\/span> (; commaIt <span style=\"color: #555555;\">!=<\/span> commaEnd; <span style=\"color: #555555;\">++<\/span>commaIt) {\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> commaIt<span style=\"color: #555555;\">-&gt;<\/span>str() <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n  }\r\n\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n\r\n  std<span style=\"color: #555555;\">::<\/span>vector<span style=\"color: #555555;\">&lt;<\/span>std<span style=\"color: #555555;\">::<\/span>string<span style=\"color: #555555;\">&gt;<\/span> resVec <span style=\"color: #555555;\">=<\/span> splitAt(text, regComma);                  <span style=\"color: #0099ff; font-style: italic;\">\/\/ (7)<\/span>\r\n  <span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #006699; font-weight: bold;\">auto<\/span> s <span style=\"color: #555555;\">:<\/span> resVec)\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> s <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span>;\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n\\n<\/span><span style=\"color: #cc3300;\">\"<\/span>;\r\n\r\n  resVec <span style=\"color: #555555;\">=<\/span> splitAt(<span style=\"color: #cc3300;\">\"abc5.4def-10.5hij2e-5klm\"<\/span>, regNumber);                    <span style=\"color: #0099ff; font-style: italic;\">\/\/ (8)<\/span>\r\n  <span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #006699; font-weight: bold;\">auto<\/span> s <span style=\"color: #555555;\">:<\/span> resVec)\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> s <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span>;\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n\\n<\/span><span style=\"color: #cc3300;\">\"<\/span>;\r\n\r\n  std<span style=\"color: #555555;\">::<\/span>regex regSpace(R<span style=\"color: #cc3300;\">\"(\\s+)\"<\/span>);\r\n  resVec <span style=\"color: #555555;\">=<\/span> splitAt(<span style=\"color: #cc3300;\">\"abc  123  456<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\t<\/span><span style=\"color: #cc3300;\">789    def hij<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n<\/span><span style=\"color: #cc3300;\">klm\"<\/span>, regSpace);           <span style=\"color: #0099ff; font-style: italic;\">\/\/ (9)<\/span>\r\n  <span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #006699; font-weight: bold;\">auto<\/span> s <span style=\"color: #555555;\">:<\/span> resVec)\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> s <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span>;\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n\\n<\/span><span style=\"color: #cc3300;\">\"<\/span>;\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Line 1 contains a string of numbers separated by commas. To get all numbers, I define in line 2 a regular expression which matches each number. All numbers include natural numbers, floating-point numbers, and numbers written in scientific notation. Line 3 defines the iterator of type <span style=\"font-family: courier new, courier;\">std::sregex_iterato<\/span>r, which gives me all tokens and displays them in line 4. The<span style=\"font-family: courier new, courier;\"> std::regex_token_iterator<\/span> in line 5 is more powerful. It searches for commas and gives me the text between them because I used the negative index -1.&nbsp;<\/p>\n<p>This pattern is so convenient that I put it in the function <span style=\"font-family: courier new, courier;\">splitAt<\/span> (line 6). <span style=\"font-family: courier new, courier;\">splitAt<\/span> takes a text and a regular expression, applies the regular expression to the text, and pushes the text between the regular expression onto the <span style=\"font-family: courier new, courier;\">std::vector&lt;std::string&gt; res<\/span>. Now, it&#8217;s pretty easy to split a text on commas (line 7), on numbers (line 8), and spaces (line 9).<\/p>\n<p>As Martin Stockmayer suggested, you can write the function <span style=\"font-family: courier new, courier;\">splitAt<\/span> more concisely, because a <span style=\"font-family: courier new, courier;\">std::vect<\/span>or can directly deal with a begin and an end iterator.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\">std<span style=\"color: #555555;\">::<\/span>vector<span style=\"color: #555555;\">&lt;<\/span>std<span style=\"color: #555555;\">::<\/span>string<span style=\"color: #555555;\">&gt;<\/span> splitAt(<span style=\"color: #006699; font-weight: bold;\">const<\/span> std<span style=\"color: #555555;\">::<\/span>string <span style=\"color: #555555;\">&amp;<\/span>text, \r\n                                 <span style=\"color: #006699; font-weight: bold;\">const<\/span> std<span style=\"color: #555555;\">::<\/span>regex <span style=\"color: #555555;\">&amp;<\/span>reg) {\r\n  <span style=\"color: #006699; font-weight: bold;\">return<\/span> std<span style=\"color: #555555;\">::<\/span>vector<span style=\"color: #555555;\">&lt;<\/span>std<span style=\"color: #555555;\">::<\/span>string<span style=\"color: #555555;\">&gt;<\/span>(std<span style=\"color: #555555;\">::<\/span>sregex_token_iterator(text.begin(), text.end(), reg, <span style=\"color: #555555;\">-<\/span><span style=\"color: #ff6600;\">1<\/span>), \r\n                                  std<span style=\"color: #555555;\">::<\/span>sregex_token_iterator());\r\n}\r\n<\/pre>\n<\/div>\n<p>The output of the program shows the expected behavior:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5747\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/regexTokenIterator.PNG\" alt=\"regexTokenIterator\" width=\"350\" height=\"396\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/regexTokenIterator.PNG 1359w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/regexTokenIterator-265x300.png 265w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/regexTokenIterator-904x1024.png 904w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/regexTokenIterator-768x870.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/regexTokenIterator-1356x1536.png 1356w\" sizes=\"auto, (max-width: 350px) 100vw, 350px\" \/><\/p>\n<p>Okay, this was my last rule for the regular expression library, and I, therefore, finished the rules for the standard library of the C++ core guidelines. But hold, there is one rule to the C standard library.<\/p>\n<\/p>\n<h2 id=\"slc1-dont-use-setjmplongjmp\"><a href=\"https:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rclib-jmp\">SL.C.1: Don\u2019t use setjmp\/longjmp<\/a><\/h2>\n<p>The reason for this rule is relatively concise:&nbsp;a <span style=\"font-family: courier new, courier;\"><code class=\"highlighter-rouge no-highlight\">longjmp<\/code> <\/span>ignores destructors, thus invalidating all resource-management strategies relying on RAII. I hope you know RAII. If not, here is the gist.&nbsp;<\/p>\n<p>RAII stands for <strong>R<\/strong>esource <strong>A<\/strong>cquisition <strong>I<\/strong>s <strong>I<\/strong>nitialization. Probably, the most crucial idiom in C++ says that a resource should be acquired in the constructor and released in the destructor of the object. The key idea is that the destructor will automatically be called if the object goes out of scope.<\/p>\n<p>The following example shows the deterministic behavior of RAII in C++.<\/p>\n<p>&nbsp;<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ raii.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;new&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;string&gt;<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">ResourceGuard<\/span>{\r\n  <span style=\"color: #9999ff;\">private:<\/span>\r\n    <span style=\"color: #006699; font-weight: bold;\">const<\/span> std<span style=\"color: #555555;\">::<\/span>string resource;\r\n  <span style=\"color: #9999ff;\">public:<\/span>\r\n    ResourceGuard(<span style=\"color: #006699; font-weight: bold;\">const<\/span> std<span style=\"color: #555555;\">::<\/span>string<span style=\"color: #555555;\">&amp;<\/span> res)<span style=\"color: #555555;\">:<\/span>resource(res){\r\n      std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Acquire the \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> resource <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\".\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span>  std<span style=\"color: #555555;\">::<\/span>endl;\r\n    }\r\n    <span style=\"color: #555555;\">~<\/span>ResourceGuard(){\r\n      std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Release the \"<\/span><span style=\"color: #555555;\">&lt;&lt;<\/span> resource <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\".\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    }\r\n};\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>(){\r\n\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n\r\n  ResourceGuard resGuard1{<span style=\"color: #cc3300;\">\"memoryBlock1\"<\/span>};            <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n<\/span><span style=\"color: #cc3300;\">Before local scope\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n  {\r\n    ResourceGuard resGuard2{<span style=\"color: #cc3300;\">\"memoryBlock2\"<\/span>};          <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\r\n  }                                                   <span style=\"color: #0099ff; font-style: italic;\">\/\/ (4)<\/span>\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"After local scope\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n  \r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n\r\n  \r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n<\/span><span style=\"color: #cc3300;\">Before try-catch block\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n  try{\r\n      ResourceGuard resGuard3{<span style=\"color: #cc3300;\">\"memoryBlock3\"<\/span>};\r\n      <span style=\"color: #006699; font-weight: bold;\">throw<\/span> std<span style=\"color: #555555;\">::<\/span>bad_alloc();                        <span style=\"color: #0099ff; font-style: italic;\">\/\/ (5)<\/span>           \r\n  }   \r\n  <span style=\"color: #006699; font-weight: bold;\">catch<\/span> (std<span style=\"color: #555555;\">::<\/span>bad_alloc<span style=\"color: #555555;\">&amp;<\/span> e){                         <span style=\"color: #0099ff; font-style: italic;\">\/\/ (6)<\/span>\r\n      std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> e.what();\r\n  }\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n<\/span><span style=\"color: #cc3300;\">After try-catch block\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n  \r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n\r\n}                                                     <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p><span style=\"font-family: courier new, courier;\">ResourceGuard<\/span> is a guard that manages its resource. In this case, the string stands for the resource. <span style=\"font-family: courier new, courier;\">ResourceGuard<\/span> creates in its constructor the resource and releases the resource in its destructor. It does its job very decent.<\/p>\n<p>The destructor of <span style=\"font-family: courier new, courier;\">resGuard1<\/span> (line 1) is called at the end of the <span style=\"font-family: courier new, courier;\">main<\/span> function (line 2). The lifetime of <span style=\"font-family: courier new, courier;\">resGuard2<\/span> (line 3) already ends in line 4. Therefore, the destructor is automatically executed. Even throwing an exception does not affect the reliability of <span style=\"font-family: courier new, courier;\">resGuard3<\/span> (line 5). The destructor is called at the end of the <span style=\"font-family: courier new, courier;\">try<\/span> block (line 6).<\/p>\n<p>The screenshot shows the lifetimes of the objects.<\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5101\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/12\/raii.png\" alt=\"raii\" width=\"400\" height=\"326\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/12\/raii.png 456w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/12\/raii-300x245.png 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/p>\n<p>I want to emphasize the critical idea of RAII: <strong>A resource&#8217;s lifetime is bound to a local variable&#8217;s lifetime, and C++ automatically manages the lifetime of locals.<\/strong><\/p>\n<p>Okay, but how can<span style=\"font-family: courier new, courier;\"> setjmp\/longjmp<\/span> break this automatism? Here is what the macro <span style=\"font-family: courier new, courier;\">setjmp<\/span> and <span style=\"font-family: courier new, courier;\">std::longjmp<\/span> does:<\/p>\n<p><strong><span style=\"font-family: courier new, courier;\">int setjmp(std::jmp_buf env):<\/span><\/strong><\/p>\n<ul>\n<li>saves the execution context&nbsp; in <span style=\"font-family: courier new, courier;\">env<\/span> for<span style=\"font-family: courier new, courier;\"> std::longjmp<\/span><\/li>\n<li>returns in its first direct invocation, 0&nbsp;<\/li>\n<li>returns in further invocations by std::lo<span style=\"font-family: courier new, courier;\">ngjmp<\/span> the value set by&nbsp;<span style=\"font-family: courier new, courier;\">std::longjmp<\/span><\/li>\n<li>it is the target for the <span style=\"font-family: courier new, courier;\">std::longjmp<\/span> call&nbsp;<\/li>\n<li>corresponds to <span style=\"font-family: courier new, courier;\">catch<\/span> in exception handling<\/li>\n<\/ul>\n<p><strong><span style=\"font-family: courier new, courier;\">void std::longjmp(std::jmp_buf env, int status):<\/span><\/strong><\/p>\n<ul>\n<li>restores the execution context stored in <span style=\"font-family: courier new, courier;\">env&nbsp;<\/span><\/li>\n<li>set the status for the <span style=\"font-family: courier new, courier;\">setjmp<\/span> call<\/li>\n<li>corresponds to <span style=\"font-family: courier new, courier;\">throw<\/span> in exception handling<\/li>\n<\/ul>\n<p>Okay, this was quite technical. Here is a simple example.<\/p>\n<p>&nbsp;<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ setJumpLongJump.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;cstdlib&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;csetjmp&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;string&gt;<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">ResourceGuard<\/span>{\r\n  <span style=\"color: #9999ff;\">private:<\/span>\r\n    <span style=\"color: #006699; font-weight: bold;\">const<\/span> std<span style=\"color: #555555;\">::<\/span>string resource;\r\n  <span style=\"color: #9999ff;\">public:<\/span>\r\n    ResourceGuard(<span style=\"color: #006699; font-weight: bold;\">const<\/span> std<span style=\"color: #555555;\">::<\/span>string<span style=\"color: #555555;\">&amp;<\/span> res)<span style=\"color: #555555;\">:<\/span>resource(res){\r\n      std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Acquire the \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> resource <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\".\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span>  std<span style=\"color: #555555;\">::<\/span>endl;\r\n    }\r\n    <span style=\"color: #555555;\">~<\/span>ResourceGuard(){\r\n      std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Release the \"<\/span><span style=\"color: #555555;\">&lt;&lt;<\/span> resource <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\".\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    }\r\n};\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>(){\r\n\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n  \r\n  <span style=\"color: #007788; font-weight: bold;\">std::jmp_buf<\/span> env;\r\n  <span style=\"color: #006699; font-weight: bold;\">volatile<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span> val;\r\n  \r\n  val <span style=\"color: #555555;\">=<\/span> setjmp(env);                                   <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n  \r\n  <span style=\"color: #006699; font-weight: bold;\">if<\/span> (val){\r\n      std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"val: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span>  val <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n      std<span style=\"color: #555555;\">::<\/span>exit(EXIT_FAILURE);\r\n  }\r\n  \r\n  {\r\n    ResourceGuard resGuard3{<span style=\"color: #cc3300;\">\"memoryBlock3\"<\/span>};           <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n    std::longjmp(env, EXIT_FAILURE);                   <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\r\n  }                                                    <span style=\"color: #0099ff; font-style: italic;\">\/\/ (4)<\/span>\r\n\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The call in line (1) saves the execution environment and returns 0. This execution environment is restored in line (3). The critical observation is that the destructor of <span style=\"font-family: courier new, courier;\">resGuard3<\/span> (line 2) is not invoked in line 4. In the concrete case, you would get a memory leak, or a mutex wouldn&#8217;t be unlocked.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5748\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/setJumpLongJump.png\" alt=\"setJumpLongJump\" width=\"350\" height=\"185\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/setJumpLongJump.png 433w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/setJumpLongJump-300x159.png 300w\" sizes=\"auto, (max-width: 350px) 100vw, 350px\" \/><\/p>\n<p>EXIT_FAILURE is the return value of the second <span style=\"font-family: courier new, courier;\">setjmp<\/span> call (line 1) and the executable&#8217;s return value.<\/p>\n<h2>What&#8217;s next?<\/h2>\n<p>DONE, but not completely! I have written over 100 posts on the main sections of the C++ core guidelines and learned a lot. Besides the main section, the guidelines also have supporting sections that sound very interesting. I will write about it in my <a href=\"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-supporting-sections\">next post<\/a>.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Before I write about the very popular RAII idiom in C++, I want to present you with a trick, which is often quite handy, when you repeatedly search for a text pattern:&nbsp; use negative search.<\/p>\n","protected":false},"author":21,"featured_media":5746,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[372],"tags":[468,469],"class_list":["post-5749","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-modern-c","tag-raii","tag-regular-expressions"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5749","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=5749"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5749\/revisions"}],"predecessor-version":[{"id":6778,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5749\/revisions\/6778"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/5746"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=5749"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=5749"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=5749"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}