<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="FeedCreator 1.8" -->
<?xml-stylesheet href="https://baszerr.eu/lib/exe/css.php?s=feed" type="text/css"?>
<rss version="2.0">
    <channel xmlns:g="http://base.google.com/ns/1.0">
        <title>BaSzErr - blog:2016:05:23</title>
        <description></description>
        <link>https://baszerr.eu/</link>
        <lastBuildDate>Thu, 30 Apr 2026 03:26:11 +0000</lastBuildDate>
        <generator>FeedCreator 1.8</generator>
        <image>
            <url>https://baszerr.eu/lib/exe/fetch.php?media=wiki:dokuwiki.svg</url>
            <title>BaSzErr</title>
            <link>https://baszerr.eu/</link>
        </image>
        <item>
            <title>delete_is_back_in_town</title>
            <link>https://baszerr.eu/doku.php?id=blog:2016:05:23:delete_is_back_in_town</link>
            <description>
&lt;h1 class=&quot;sectionedit1&quot; id=&quot;delete_is_back_in_town&quot;&gt;2016-05-23 - delete is back in town&lt;/h1&gt;
&lt;div class=&quot;level1&quot;&gt;

&lt;p&gt;
back in &lt;a href=&quot;https://en.wikipedia.org/wiki/C++98&quot; class=&quot;interwiki iw_wp&quot; title=&quot;https://en.wikipedia.org/wiki/C++98&quot;&gt;C++98&lt;/a&gt; times, during code review, my rule of thumb was: “&lt;em&gt;if there is a &lt;strong&gt;delete&lt;/strong&gt; in your code, you probably did something wrong&lt;/em&gt;”. they idea is that smart pointers ought to be used for managing lifetime of dynamically allocated memory. using raw pointers with ownership is literally asking for troubles.
&lt;/p&gt;

&lt;p&gt;
time passed on and now we have &lt;a href=&quot;https://en.wikipedia.org/wiki/C++14&quot; class=&quot;interwiki iw_wp&quot; title=&quot;https://en.wikipedia.org/wiki/C++14&quot;&gt;C++14&lt;/a&gt;. the smart pointers note still holds. in fact it holds even more, thanks to move-semantics! what is changed is having &lt;em&gt;delete&lt;/em&gt; in code base is not a bad thing any more. it is bad, if it used for memory. one common example is class that is non-copyable and non-movable:
&lt;/p&gt;
&lt;pre class=&quot;code c&quot;&gt;&lt;span class=&quot;kw4&quot;&gt;struct&lt;/span&gt; NoCopyNoMove
&lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
  &lt;span class=&quot;co1&quot;&gt;// disallow copying:&lt;/span&gt;
  NoCopyNoMove&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;NoCopyNoMove const&lt;span class=&quot;sy0&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span class=&quot;sy0&quot;&gt;=&lt;/span&gt; delete&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
  NoCopyNoMove&lt;span class=&quot;sy0&quot;&gt;&amp;amp;&lt;/span&gt; operator&lt;span class=&quot;sy0&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;NoCopyNoMove const&lt;span class=&quot;sy0&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span class=&quot;sy0&quot;&gt;=&lt;/span&gt; delete&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;co1&quot;&gt;// disallow moving:&lt;/span&gt;
  NoCopyNoMove&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;NoCopyNoMove&lt;span class=&quot;sy0&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span class=&quot;sy0&quot;&gt;=&lt;/span&gt; delete&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
  NoCopyNoMove&lt;span class=&quot;sy0&quot;&gt;&amp;amp;&lt;/span&gt; operator&lt;span class=&quot;sy0&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;NoCopyNoMove&lt;span class=&quot;sy0&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span class=&quot;sy0&quot;&gt;=&lt;/span&gt; delete&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;
it&amp;#039;s a canonical example. common code nowadays. not everyone do however &lt;em&gt;delete&lt;/em&gt; can be used in a very creative way, to disallow invalid code. do you see a problem in the code below?
&lt;/p&gt;
&lt;pre class=&quot;code c&quot;&gt;&lt;span class=&quot;co2&quot;&gt;#include &amp;lt;string&amp;gt;&lt;/span&gt;
&lt;span class=&quot;co2&quot;&gt;#include &amp;lt;iostream&amp;gt;&lt;/span&gt;
&amp;nbsp;
&lt;span class=&quot;kw4&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;kw4&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;*&lt;/span&gt; f&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;std&lt;span class=&quot;sy0&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;me2&quot;&gt;string&lt;/span&gt; const&lt;span class=&quot;sy0&quot;&gt;&amp;amp;&lt;/span&gt; s&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;return&lt;/span&gt; s.&lt;span class=&quot;me1&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
&lt;span class=&quot;kw4&quot;&gt;int&lt;/span&gt; main&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
  &lt;span class=&quot;kw4&quot;&gt;auto&lt;/span&gt; ptr &lt;span class=&quot;sy0&quot;&gt;=&lt;/span&gt; f&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;st0&quot;&gt;&amp;quot;test string for debugging&amp;quot;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
  std&lt;span class=&quot;sy0&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;me2&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;sy0&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; ptr &lt;span class=&quot;sy0&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;st0&quot;&gt;&amp;quot;&lt;span class=&quot;es1&quot;&gt;\n&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;
neither gcc nor clang did not notice any issue. address sanitizer did helper with a colorful kaboom note:
&lt;/p&gt;

&lt;p&gt;
&lt;a href=&quot;https://baszerr.eu/lib/exe/detail.php?id=blog%3A2016%3A05%3A23%3Adelete_is_back_in_town&amp;amp;media=blog:2016:05:23:asan_error_from_invalid_pointer.png&quot; class=&quot;media&quot; title=&quot;blog:2016:05:23:asan_error_from_invalid_pointer.png&quot;&gt;&lt;img src=&quot;https://baszerr.eu/lib/exe/fetch.php?media=blog:2016:05:23:asan_error_from_invalid_pointer.png&quot; class=&quot;media&quot; loading=&quot;lazy&quot; title=&quot;asan error report&quot; alt=&quot;asan error report&quot; /&gt;&lt;/a&gt;
&lt;/p&gt;

&lt;p&gt;
the problem is line:
&lt;/p&gt;
&lt;pre class=&quot;code c&quot;&gt;&lt;span class=&quot;kw4&quot;&gt;auto&lt;/span&gt; ptr &lt;span class=&quot;sy0&quot;&gt;=&lt;/span&gt; f&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;st0&quot;&gt;&amp;quot;test string for debugging&amp;quot;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;
that actually returns a pointer to a &lt;strong&gt;temporary&lt;/strong&gt; &lt;em&gt;std::string&lt;/em&gt;, passed as an argument to &lt;em&gt;f()&lt;/em&gt;. note that function &lt;em&gt;f()&lt;/em&gt; is itself valid – it operates on data it got as an argument. it&amp;#039;s a caller&amp;#039;s code that is wrong! compiler does not help… or could it?
&lt;/p&gt;

&lt;p&gt;
it actually turns out you can turn this tricky, undefined behavior, run time problem… into a trivial compile time error! what you want is &lt;em&gt;f()&lt;/em&gt; does not work, when it is given a temporary! this is just a single line in C++:
&lt;/p&gt;
&lt;pre class=&quot;code c&quot;&gt;&lt;span class=&quot;co2&quot;&gt;#include &amp;lt;string&amp;gt;&lt;/span&gt;
&lt;span class=&quot;co2&quot;&gt;#include &amp;lt;iostream&amp;gt;&lt;/span&gt;
&amp;nbsp;
&lt;span class=&quot;kw4&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;kw4&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;*&lt;/span&gt; f&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;std&lt;span class=&quot;sy0&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;me2&quot;&gt;string&lt;/span&gt; const&lt;span class=&quot;sy0&quot;&gt;&amp;amp;&lt;/span&gt; s&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;return&lt;/span&gt; s.&lt;span class=&quot;me1&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;
&lt;span class=&quot;kw4&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;kw4&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;*&lt;/span&gt; f&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;std&lt;span class=&quot;sy0&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;me2&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; s&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span class=&quot;sy0&quot;&gt;=&lt;/span&gt; delete&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;co1&quot;&gt;// &amp;lt;-- ... and fixed!&lt;/span&gt;
&amp;nbsp;
&lt;span class=&quot;kw4&quot;&gt;int&lt;/span&gt; main&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
  &lt;span class=&quot;kw4&quot;&gt;auto&lt;/span&gt; ptr &lt;span class=&quot;sy0&quot;&gt;=&lt;/span&gt; f&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;st0&quot;&gt;&amp;quot;test string for debugging&amp;quot;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
  std&lt;span class=&quot;sy0&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;me2&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;sy0&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; ptr &lt;span class=&quot;sy0&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;st0&quot;&gt;&amp;quot;&lt;span class=&quot;es1&quot;&gt;\n&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;
now when you try to compile, you get nice, readable, self-describing error message:
&lt;/p&gt;

&lt;p&gt;
&lt;a href=&quot;https://baszerr.eu/lib/exe/detail.php?id=blog%3A2016%3A05%3A23%3Adelete_is_back_in_town&amp;amp;media=blog:2016:05:23:compile_error_on_delete.png&quot; class=&quot;media&quot; title=&quot;blog:2016:05:23:compile_error_on_delete.png&quot;&gt;&lt;img src=&quot;https://baszerr.eu/lib/exe/fetch.php?media=blog:2016:05:23:compile_error_on_delete.png&quot; class=&quot;media&quot; loading=&quot;lazy&quot; title=&quot;compile error from clang++&quot; alt=&quot;compile error from clang++&quot; /&gt;&lt;/a&gt;
&lt;/p&gt;

&lt;p&gt;
aha! so we&amp;#039;re trying to use a function, returning pointer from an argument object, with a temporary that gets invalidated! let me fix that:
&lt;/p&gt;
&lt;pre class=&quot;code c&quot;&gt;&lt;span class=&quot;co2&quot;&gt;#include &amp;lt;string&amp;gt;&lt;/span&gt;
&lt;span class=&quot;co2&quot;&gt;#include &amp;lt;iostream&amp;gt;&lt;/span&gt;
&amp;nbsp;
&lt;span class=&quot;kw4&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;kw4&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;*&lt;/span&gt; f&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;std&lt;span class=&quot;sy0&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;me2&quot;&gt;string&lt;/span&gt; const&lt;span class=&quot;sy0&quot;&gt;&amp;amp;&lt;/span&gt; s&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;return&lt;/span&gt; s.&lt;span class=&quot;me1&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;
&lt;span class=&quot;kw4&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;kw4&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;*&lt;/span&gt; f&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;std&lt;span class=&quot;sy0&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;me2&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; s&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span class=&quot;sy0&quot;&gt;=&lt;/span&gt; delete&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
&lt;span class=&quot;kw4&quot;&gt;int&lt;/span&gt; main&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
  &lt;span class=&quot;kw4&quot;&gt;auto&lt;/span&gt; str &lt;span class=&quot;sy0&quot;&gt;=&lt;/span&gt; std&lt;span class=&quot;sy0&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;me2&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;&lt;span class=&quot;st0&quot;&gt;&amp;quot;test dsfjsdlf sdf sd fsdf&amp;quot;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;kw4&quot;&gt;auto&lt;/span&gt; ptr &lt;span class=&quot;sy0&quot;&gt;=&lt;/span&gt; f&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;str&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;co1&quot;&gt;// &amp;lt;-- non-temporary object passed for processing&lt;/span&gt;
  std&lt;span class=&quot;sy0&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;me2&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;sy0&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; ptr &lt;span class=&quot;sy0&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;st0&quot;&gt;&amp;quot;&lt;span class=&quot;es1&quot;&gt;\n&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;
+1 for code that cannot be used incorrectly. :) &lt;em&gt;delete&lt;/em&gt; is back in town – and it&amp;#039;s &lt;strong&gt;BIG&lt;/strong&gt;! :)
&lt;/p&gt;

&lt;/div&gt;
</description>
            <author>anonymous@undisclosed.example.com (Anonymous)</author>
            <pubDate>Tue, 15 Jun 2021 20:09:21 +0000</pubDate>
        </item>
    </channel>
</rss>
