<?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:2014:04:24</title>
        <description></description>
        <link>https://baszerr.eu/</link>
        <lastBuildDate>Tue, 05 May 2026 00:50:26 +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>gcc_resource_leak</title>
            <link>https://baszerr.eu/doku.php?id=blog:2014:04:24:gcc_resource_leak</link>
            <description>
&lt;h1 class=&quot;sectionedit1&quot; id=&quot;gcc_resource_leak&quot;&gt;2014-04-24 - gcc resource leak&lt;/h1&gt;
&lt;div class=&quot;level1&quot;&gt;

&lt;p&gt;
recently at work i had a problem with resource leak in one of my tests. it took me a while to notice that gcc does not generate proper code, if exception is triggered, while doing bracket-initialization of an object. problem is nicely &lt;a href=&quot;https://baszerr.eu/lib/exe/fetch.php?media=blog:2014:04:24:leak.cpp&quot; class=&quot;media mediafile mf_cpp&quot; title=&quot;blog:2014:04:24:leak.cpp (501 B)&quot;&gt;illustrated in a code&lt;/a&gt; below:
&lt;/p&gt;
&lt;pre class=&quot;code cpp&quot;&gt;&lt;span class=&quot;co2&quot;&gt;#include &amp;lt;iostream&amp;gt;&lt;/span&gt;
&amp;nbsp;
&lt;span class=&quot;kw2&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;kw2&quot;&gt;namespace&lt;/span&gt; std&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
&lt;span class=&quot;kw4&quot;&gt;struct&lt;/span&gt; Data
&lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
  Data&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;kw4&quot;&gt;int&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;kw1&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;nu0&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;
  Data&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;kw4&quot;&gt;void&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;kw3&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;sy1&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; __PRETTY_FUNCTION__ &lt;span class=&quot;sy1&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; endl&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;
  Data&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;Data &lt;span class=&quot;kw4&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;sy3&quot;&gt;&amp;amp;&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;kw3&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;sy1&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; __PRETTY_FUNCTION__ &lt;span class=&quot;sy1&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; endl&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;
  Data&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;Data&lt;span class=&quot;sy3&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;br0&quot;&gt;&amp;#123;&lt;/span&gt; &lt;span class=&quot;kw3&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;sy1&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; __PRETTY_FUNCTION__ &lt;span class=&quot;sy1&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; endl&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;
  ~Data&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;kw4&quot;&gt;void&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;kw3&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;sy1&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; __PRETTY_FUNCTION__ &lt;span class=&quot;sy1&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; endl&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
&lt;span class=&quot;kw4&quot;&gt;struct&lt;/span&gt; More
&lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
  Data a_&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt;
  Data b_&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
More foo&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;kw4&quot;&gt;void&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;kw1&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt; Data&lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;, Data&lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;co1&quot;&gt;// &amp;lt;-- here is the problem&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;kw4&quot;&gt;void&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;kw2&quot;&gt;try&lt;/span&gt;
  &lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
    foo&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;sy4&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;
  &lt;span class=&quot;kw2&quot;&gt;catch&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;br0&quot;&gt;&amp;#123;&lt;/span&gt;
    &lt;span class=&quot;kw3&quot;&gt;cerr&lt;/span&gt; &lt;span class=&quot;sy1&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;st0&quot;&gt;&amp;quot;exception&amp;quot;&lt;/span&gt; &lt;span class=&quot;sy1&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; endl&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;
while constructing object &lt;em&gt;More&lt;/em&gt;, in function &lt;em&gt;foo()&lt;/em&gt;, if second parameter throws, previous object is never destroyed, so the output is:
&lt;/p&gt;
&lt;pre class=&quot;code&quot;&gt;Data::Data()
exception&lt;/pre&gt;

&lt;p&gt;
instead of (clang gives valid result, btw):
&lt;/p&gt;
&lt;pre class=&quot;code&quot;&gt;Data::Data()
Data::~Data()
exception&lt;/pre&gt;

&lt;p&gt;
simply removing argument from c-tor of a second object (i.e. no exception) outputs proper order:
&lt;/p&gt;
&lt;pre class=&quot;code&quot;&gt;Data::Data()
Data::Data()
Data::~Data()
Data::~Data()&lt;/pre&gt;

&lt;p&gt;
i&amp;#039;ve discovered this issue in GCC 4.7, confirmed in 4.8 and was (in a way) happy to file a report… when i discovered it has &lt;a href=&quot;http://gcc.gnu.org/bugzilla//show_bug.cgi?id=57930&quot; class=&quot;urlextern&quot; title=&quot;http://gcc.gnu.org/bugzilla//show_bug.cgi?id=57930&quot; rel=&quot;ugc nofollow&quot;&gt;already been reported&lt;/a&gt;. almost a year ago. to make it worse, GCC 4.9 has just been released and the bug is still in “new” state (not even assigned). haven&amp;#039;t tested with 4.9, though most likely it still won&amp;#039;t work… bad news. :(
&lt;/p&gt;

&lt;/div&gt;
</description>
            <author>anonymous@undisclosed.example.com (Anonymous)</author>
            <pubDate>Tue, 15 Jun 2021 20:08:57 +0000</pubDate>
        </item>
        <item>
            <title>measuring_memory_overhead</title>
            <link>https://baszerr.eu/doku.php?id=blog:2014:04:24:measuring_memory_overhead</link>
            <description>
&lt;h1 class=&quot;sectionedit1&quot; id=&quot;measuring_memory_overhead&quot;&gt;2014-04-24 - measuring memory overhead&lt;/h1&gt;
&lt;div class=&quot;level1&quot;&gt;

&lt;p&gt;
recently i was preparing a training on &lt;a href=&quot;https://en.wikipedia.org/wiki/RAII&quot; class=&quot;interwiki iw_wp&quot; title=&quot;https://en.wikipedia.org/wiki/RAII&quot;&gt;RAII&lt;/a&gt; and using it to handle resources (most commonly – memory). as a part of it &lt;a href=&quot;https://en.wikipedia.org/wiki/C++11&quot; class=&quot;interwiki iw_wp&quot; title=&quot;https://en.wikipedia.org/wiki/C++11&quot;&gt;C++11&amp;#039;s&lt;/a&gt; smart pointers were to be presented. i wanted to show why &lt;em&gt;std::make_shared&amp;lt;T&amp;gt;()&lt;/em&gt; is better that &lt;em&gt;std::shared_ptr&amp;lt;T&amp;gt;{new T}&lt;/em&gt;. &lt;a href=&quot;http://channel9.msdn.com/Tags/stephan-t-lavavej&quot; class=&quot;urlextern&quot; title=&quot;http://channel9.msdn.com/Tags/stephan-t-lavavej&quot; rel=&quot;ugc nofollow&quot;&gt;STL&lt;/a&gt; gave &lt;a href=&quot;http://channel9.msdn.com/Series/C9-Lectures-Stephan-T-Lavavej-Standard-Template-Library-STL-/C9-Lectures-Stephan-T-Lavavej-Standard-Template-Library-STL-3-of-n&quot; class=&quot;urlextern&quot; title=&quot;http://channel9.msdn.com/Series/C9-Lectures-Stephan-T-Lavavej-Standard-Template-Library-STL-/C9-Lectures-Stephan-T-Lavavej-Standard-Template-Library-STL-3-of-n&quot; rel=&quot;ugc nofollow&quot;&gt;a nice explanation of make_shared memory optimization&lt;/a&gt;. how to easily show it takes less allocations and so the allocation is smaller? i got lazy (surprise! surprise! nobody expected spanish inquisition!) and decided just to use &lt;a href=&quot;https://en.wikipedia.org/wiki/valgrind&quot; class=&quot;interwiki iw_wp&quot; title=&quot;https://en.wikipedia.org/wiki/valgrind&quot;&gt;valgrind&lt;/a&gt; for that. one could easily see that when using &lt;em&gt;make_shared&lt;/em&gt; there is 1 allocation, compared to 2, when &lt;em&gt;shared_ptr&lt;/em&gt; was created directly – clearly a gain.
&lt;/p&gt;

&lt;p&gt;
unfortunately this approach did not manage to show that, in fact, memory is saved as well. the problem (?) was that valgrind reports exactly how many bytes are &lt;em class=&quot;u&quot;&gt;requested&lt;/em&gt; by the application – not how many would be assigned by actual allocator. but still, the total size should always be the same, right? and here comes the funny part – no, it may not. because of performance reasons &lt;em&gt;shared_ptr&lt;/em&gt; use atomics for counting both ref-counters. on most architectures this means that memory for those need to be aligned properly and so the single allocation need to include proper padding. so having 2 64bit atomic counters, gives 16B of must-be-aligned-storage. but what if &lt;em&gt;make_shared&lt;/em&gt; allocates user data and counters at once? well – just put counters first and we&amp;#039;re done (allocation is always properly aligned). well – not really… if you allocate an array of such a structures, they need to be continuous and aligned. so here is where ending padding kicks in. assuming we need 8B alignment, 2*8B=16B for counters and, say 20B for user-data, we get 36B data structure. now we need a 4B padding so that next one in an array will be aligned as well. the final size of such a structure is 40B.
&lt;/p&gt;

&lt;p&gt;
&lt;a href=&quot;https://baszerr.eu/lib/exe/detail.php?id=blog%3A2014%3A04%3A24%3Ameasuring_memory_overhead&amp;amp;media=blog:2014:04:24:allocation.png&quot; class=&quot;media&quot; title=&quot;blog:2014:04:24:allocation.png&quot;&gt;&lt;img src=&quot;https://baszerr.eu/lib/exe/fetch.php?media=blog:2014:04:24:allocation.png&quot; class=&quot;mediacenter&quot; loading=&quot;lazy&quot; title=&quot;single allocation&quot; alt=&quot;single allocation&quot; /&gt;&lt;/a&gt;
&lt;/p&gt;

&lt;p&gt;
of course doing separate allocations, and assuming user data needs not to be aligned, we can have two allocations: 16B for counters and 20B for user-data. having perfect allocator it would be so. in real life, for small allocations, we usually get way more than we asked for. this means that usually asking for 16B is no different (from the system PoV) than asking for, say 64B. using &lt;em&gt;make_shared&lt;/em&gt; we always save time (2x less allocations) and most of the time also memory, since we pay this overhead just once and padding disappears in this gap.
&lt;/p&gt;

&lt;p&gt;
finally, for the purpose of the training, i&amp;#039;ve measured exact memory usage using modified program, that keeps all allocated memory in a container, and measuring process memory usage before exiting. for my sample data memory overhead of directly creating &lt;em&gt;shared_ptr&lt;/em&gt;, compared to using &lt;em&gt;make_shared&lt;/em&gt;, was about 20%. time overhead was nearly 100%, as one could expect, since most of the time program spent in allocators and deallocators. be careful what and how is being measured – there are things going on behind the scenes… ;)
&lt;/p&gt;

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