<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="FeedCreator 1.8" -->
<?xml-stylesheet href="https://www.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:2012:04:06</title>
        <description></description>
        <link>https://www.baszerr.eu/</link>
        <lastBuildDate>Wed, 06 May 2026 09:38:03 +0000</lastBuildDate>
        <generator>FeedCreator 1.8</generator>
        <image>
            <url>https://www.baszerr.eu/lib/exe/fetch.php?media=wiki:dokuwiki.svg</url>
            <title>BaSzErr</title>
            <link>https://www.baszerr.eu/</link>
        </image>
        <item>
            <title>1</title>
            <link>https://www.baszerr.eu/doku.php?id=blog:2012:04:06:1</link>
            <description>
&lt;h1 class=&quot;sectionedit1&quot; id=&quot;measurements&quot;&gt;2012.04.06 - measurements&lt;/h1&gt;
&lt;div class=&quot;level1&quot;&gt;

&lt;p&gt;
&lt;a href=&quot;https://www.baszerr.eu/lib/exe/detail.php?id=blog%3A2012%3A04%3A06%3A1&amp;amp;media=blog:2012:04:06:ruler_macro.jpg&quot; class=&quot;media&quot; title=&quot;blog:2012:04:06:ruler_macro.jpg&quot;&gt;&lt;img src=&quot;https://www.baszerr.eu/lib/exe/fetch.php?w=300&amp;amp;tok=f4d7f3&amp;amp;media=blog:2012:04:06:ruler_macro.jpg&quot; class=&quot;mediaright&quot; align=&quot;right&quot; loading=&quot;lazy&quot; title=&quot;ruler (image taken from http://1.bp.blogspot.com/-SBuXK-OHXno/TlVY2hirTFI/AAAAAAAABvE/uqNf-Ic5TdE/s1600/ruler_macro.jpg)&quot; alt=&quot;ruler (image taken from http://1.bp.blogspot.com/-SBuXK-OHXno/TlVY2hirTFI/AAAAAAAABvE/uqNf-Ic5TdE/s1600/ruler_macro.jpg)&quot; width=&quot;300&quot; /&gt;&lt;/a&gt;
i&amp;#039;ve wrote about &lt;a href=&quot;https://www.baszerr.eu/doku.php?id=blog:2012:02:26:1&quot; class=&quot;wikilink1&quot; title=&quot;blog:2012:02:26:1&quot; data-wiki-id=&quot;blog:2012:02:26:1&quot;&gt;the need for measuring&lt;/a&gt;. quite a &lt;a href=&quot;https://www.baszerr.eu/doku.php?id=blog:2011:07:08:1&quot; class=&quot;wikilink1&quot; title=&quot;blog:2011:07:08:1&quot; data-wiki-id=&quot;blog:2011:07:08:1&quot;&gt;few times&lt;/a&gt; actually… it is so obvious, for an engineer, to perform an experiment to “see what happens”… or is it?
&lt;/p&gt;

&lt;p&gt;
it appears that not that many engineers does that after all. that is why superstitions still lives on. “templates are bad”&lt;sup&gt;&lt;a href=&quot;#fn__1&quot; id=&quot;fnt__1&quot; class=&quot;fn_top&quot;&gt;1)&lt;/a&gt;&lt;/sup&gt;, “exceptions have terrible impact on performance”, “if you use exceptions your program has an errors” and a lot, lot more bullshit bouncing here and there, on the internet and among programmers. some time ago i was “charged” of wasting time, while doing measurements!
&lt;/p&gt;

&lt;p&gt;
if you&amp;#039;re sure about your opinions, why not to check it with the proper experiment(s)? and if you have measured this 5 years ago, how come are you sure your results still holds? i highly recommend &lt;a href=&quot;https://en.wikipedia.org/wiki/Bjarne Stroustrup&quot; class=&quot;interwiki iw_wp&quot; title=&quot;https://en.wikipedia.org/wiki/Bjarne Stroustrup&quot;&gt;Bjarne Stroustrup&lt;/a&gt;&amp;#039;s &lt;a href=&quot;http://channel9.msdn.com/Events/GoingNative/GoingNative-2012/Keynote-Bjarne-Stroustrup-Cpp11-Style&quot; class=&quot;urlextern&quot; title=&quot;http://channel9.msdn.com/Events/GoingNative/GoingNative-2012/Keynote-Bjarne-Stroustrup-Cpp11-Style&quot; rel=&quot;ugc nofollow&quot;&gt;C++11 style&lt;/a&gt; lecture, presented during &lt;a href=&quot;http://channel9.msdn.com/Events/GoingNative/GoingNative-2012&quot; class=&quot;urlextern&quot; title=&quot;http://channel9.msdn.com/Events/GoingNative/GoingNative-2012&quot; rel=&quot;ugc nofollow&quot;&gt;Going native 2012&lt;/a&gt; conference&lt;sup&gt;&lt;a href=&quot;#fn__2&quot; id=&quot;fnt__2&quot; class=&quot;fn_top&quot;&gt;2)&lt;/a&gt;&lt;/sup&gt;. for the matter of this text, pay a special attention to the part from 44:50 to 52:48 of &lt;a href=&quot;http://channel9.msdn.com/Events/GoingNative/GoingNative-2012/Keynote-Bjarne-Stroustrup-Cpp11-Style&quot; class=&quot;urlextern&quot; title=&quot;http://channel9.msdn.com/Events/GoingNative/GoingNative-2012/Keynote-Bjarne-Stroustrup-Cpp11-Style&quot; rel=&quot;ugc nofollow&quot;&gt;Bjarne&amp;#039;s the lecture&lt;/a&gt;. since on that particular presentation important graph disappeared, along watching see &lt;a href=&quot;http://ecn.channel9.msdn.com/events/GoingNative12/GN12Cpp11Style.pdf&quot; class=&quot;urlextern&quot; title=&quot;http://ecn.channel9.msdn.com/events/GoingNative12/GN12Cpp11Style.pdf&quot; rel=&quot;ugc nofollow&quot;&gt;the C++11 Style presentation&lt;/a&gt;, starting with page 43. &lt;em&gt;now you have a time to watch it. i mean it. do it now – i&amp;#039;ll wait!&lt;/em&gt; ok so if you&amp;#039;ve seen it – what was YOUR first reaction? what result did YOU expect? well – my expectation was wrong too… the funny thing is, as Bjarne noticed, it was so for everyone. times have changed and &lt;a href=&quot;https://www.baszerr.eu/doku.php?id=docs:cache_usage_effects_on_modern_cpus:cache_usage_effects_on_modern_cpus&quot; class=&quot;wikilink1&quot; title=&quot;docs:cache_usage_effects_on_modern_cpus:cache_usage_effects_on_modern_cpus&quot; data-wiki-id=&quot;docs:cache_usage_effects_on_modern_cpus:cache_usage_effects_on_modern_cpus&quot;&gt;CPUs cache is now a crucial resource&lt;/a&gt;. if your expectations was that the list will be faster around 100-10k elements – it WAS true… about 10 years ago.
&lt;/p&gt;

&lt;p&gt;
what just happened? knowledge outdated. this is normal. 10 years is quite a time, actually… :) and this is the clue of what i want to share – &lt;strong&gt;MEASURE and SAVE&lt;/strong&gt; your test programs for future! even if you checked and confirmed your opinion it does not mean that a new compiler or a newer CPU won&amp;#039;t make the results dramatically different – even counter intuitive.
&lt;/p&gt;

&lt;p&gt;
in my opinion each project should have one more directory, along “src”, “tests”, “doc” and similar, namely “measurements”. this would be the location where a lot of (short) “checker” programs will be held, that check a number of means to solve each problem, team has decided to measure. this can be memory usage, run time or even as simple as output binary size. with rational description (simple documentation) on what and how is being measured, this can be used to periodically ensure that foundations of solutions being used still holds.
&lt;/p&gt;

&lt;p&gt;
think about it as a way of extending an idea of automated testing, for the conceptual (design) decisions. tests are run at least daily, by &lt;a href=&quot;https://en.wikipedia.org/wiki/continuous integration&quot; class=&quot;interwiki iw_wp&quot; title=&quot;https://en.wikipedia.org/wiki/continuous integration&quot;&gt;CI&lt;/a&gt; tool. “measurements” tests can be run the same way, but in longer time spans – each time toolchain and/or hardware changed + every half a year (keep in mind, that if you&amp;#039;re supposed to do it manually and rarely, you probably will forget about it ;)). cycle is simple: measure, save, re-measure and compare. results can be astonishing…
&lt;/p&gt;

&lt;/div&gt;
&lt;div class=&quot;footnotes&quot;&gt;
&lt;div class=&quot;fn&quot;&gt;&lt;sup&gt;&lt;a href=&quot;#fnt__1&quot; id=&quot;fn__1&quot; class=&quot;fn_bot&quot;&gt;1)&lt;/a&gt;&lt;/sup&gt; 
&lt;div class=&quot;content&quot;&gt;whatever does this mean…&lt;/div&gt;&lt;/div&gt;
&lt;div class=&quot;fn&quot;&gt;&lt;sup&gt;&lt;a href=&quot;#fnt__2&quot; id=&quot;fn__2&quot; class=&quot;fn_bot&quot;&gt;2)&lt;/a&gt;&lt;/sup&gt; 
&lt;div class=&quot;content&quot;&gt;this particular lecture was also &lt;a href=&quot;https://www.baszerr.eu/doku.php?id=blog:2012:03:16:1&quot; class=&quot;wikilink1&quot; title=&quot;blog:2012:03:16:1&quot; data-wiki-id=&quot;blog:2012:03:16:1&quot;&gt;performed in Wrocław recently&lt;/a&gt;.&lt;/div&gt;&lt;/div&gt;
&lt;/div&gt;
</description>
            <author>anonymous@undisclosed.example.com (Anonymous)</author>
            <pubDate>Tue, 15 Jun 2021 20:09:17 +0000</pubDate>
        </item>
        <item>
            <title>2</title>
            <link>https://www.baszerr.eu/doku.php?id=blog:2012:04:06:2</link>
            <description>
&lt;h1 class=&quot;sectionedit1&quot; id=&quot;undefined_behavior_is_bad&quot;&gt;2012.04.06 - &amp;quot;undefined behavior&amp;quot; IS bad&lt;/h1&gt;
&lt;div class=&quot;level1&quot;&gt;

&lt;p&gt;
among projects i worked on, there were cases, where &lt;a href=&quot;https://en.wikipedia.org/wiki/undefined behavior&quot; class=&quot;interwiki iw_wp&quot; title=&quot;https://en.wikipedia.org/wiki/undefined behavior&quot;&gt;undefined behavior&lt;/a&gt; was exploited, to achieve something useful. just to mention one case, when memory was allocated with &lt;em&gt;new MyClass&lt;/em&gt; and freed… using &lt;em&gt;free(void*)&lt;/em&gt;. this was (still is?) done in the main messaging framework of the million-line project. for compiler version in use, it happened to work, as long as &lt;em&gt;MyClass&lt;/em&gt; had POD-only members&lt;sup&gt;&lt;a href=&quot;#fn__1&quot; id=&quot;fnt__1&quot; class=&quot;fn_top&quot;&gt;1)&lt;/a&gt;&lt;/sup&gt;.
&lt;/p&gt;

&lt;p&gt;
i was always against such a hacks and promoted solutions that would not require it. in practice most of the times the answer was “it&amp;#039;s working so don&amp;#039;t touch it”. very ignorant approach at least…
&lt;/p&gt;

&lt;p&gt;
when something is UB, you should see big red sign, that says: “if you cross this line ANYTHING can happen”. and so yesterday i came across a guy on the &lt;a href=&quot;https://en.wikipedia.org/wiki/usenet&quot; class=&quot;interwiki iw_wp&quot; title=&quot;https://en.wikipedia.org/wiki/usenet&quot;&gt;usenet&lt;/a&gt;, posting that his few-lines program does not work when compiled with optimizations on &lt;a href=&quot;https://en.wikipedia.org/wiki/GCC (software)&quot; class=&quot;interwiki iw_wp&quot; title=&quot;https://en.wikipedia.org/wiki/GCC (software)&quot;&gt;GCC&lt;/a&gt; 4.6. here it is:
&lt;/p&gt;
&lt;pre class=&quot;code C&quot;&gt;&lt;span class=&quot;co2&quot;&gt;#include&amp;lt;cstdlib&amp;gt;&lt;/span&gt;
&lt;span class=&quot;co2&quot;&gt;#include&amp;lt;cstdio&amp;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;kw4&quot;&gt;int&lt;/span&gt; argc&lt;span class=&quot;sy0&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kw4&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;sy0&quot;&gt;*&lt;/span&gt;argv&lt;span class=&quot;br0&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#93;&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;a href=&quot;http://www.opengroup.org/onlinepubs/009695399/functions/printf.html&quot;&gt;&lt;span class=&quot;kw3&quot;&gt;printf&lt;/span&gt;&lt;/a&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;st0&quot;&gt;&amp;quot;test&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;span class=&quot;kw4&quot;&gt;int&lt;/span&gt; t &lt;span class=&quot;sy0&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nu0&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kw1&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt; &lt;span class=&quot;kw4&quot;&gt;int&lt;/span&gt; i&lt;span class=&quot;sy0&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt; i&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;30000&lt;/span&gt; &lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt; i&lt;span class=&quot;sy0&quot;&gt;++&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;
        &lt;span class=&quot;kw1&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt; &lt;span class=&quot;kw4&quot;&gt;int&lt;/span&gt; j&lt;span class=&quot;sy0&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt; j&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;10000&lt;/span&gt; &lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt; j&lt;span class=&quot;sy0&quot;&gt;++&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;
            t &lt;span class=&quot;sy0&quot;&gt;+=&lt;/span&gt; j&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
    &lt;a href=&quot;http://www.opengroup.org/onlinepubs/009695399/functions/printf.html&quot;&gt;&lt;span class=&quot;kw3&quot;&gt;printf&lt;/span&gt;&lt;/a&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;st0&quot;&gt;&amp;quot;%d&amp;quot;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;,&lt;/span&gt;t&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;kw1&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nu0&quot;&gt;0&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;
when compiled with
&lt;/p&gt;
&lt;pre class=&quot;code&quot;&gt;g++ -Wall x.cpp&lt;/pre&gt;

&lt;p&gt;
everything works fine – program executes and prints the result on the screen. adding optimizations, however causes program to loop forever. namely, if compiled with:
&lt;/p&gt;
&lt;pre class=&quot;code&quot;&gt;g++ -Wall x.cpp -O3&lt;/pre&gt;

&lt;p&gt;
program never ends. what is the problem with this code? it uses UB of overflowing signed integer. adding &lt;em&gt;-fno-strict-overflow&lt;/em&gt; (i.e. of-the-standard extension, making signed integer overflows defined) solves problem – compiler computes value in compile-time and puts a constant value in the code.
&lt;/p&gt;

&lt;p&gt;
using the wrong side of force is asking for a trouble. if you do so, sooner or later you&amp;#039;ll bounce from the wall, spend hours (or days) debugging just to find out you&amp;#039;re not &lt;abbr title=&quot;specification&quot;&gt;spec&lt;/abbr&gt;-compliant and having to fix code, that should have been written the right way from the very beginning. is it worth it?
&lt;/p&gt;

&lt;p&gt;
btw: GCC 4.7 gives the same results as 4.6, while GCC 4.5 and Clang++ 3.0 gives “expected” results. unless you&amp;#039;re an oracle, forget about using UBs!.
&lt;/p&gt;

&lt;/div&gt;
&lt;div class=&quot;footnotes&quot;&gt;
&lt;div class=&quot;fn&quot;&gt;&lt;sup&gt;&lt;a href=&quot;#fnt__1&quot; id=&quot;fn__1&quot; class=&quot;fn_bot&quot;&gt;1)&lt;/a&gt;&lt;/sup&gt; 
&lt;div class=&quot;content&quot;&gt;btw: &lt;em&gt;MyClass&lt;/em&gt; was not a POD though.&lt;/div&gt;&lt;/div&gt;
&lt;/div&gt;
</description>
            <author>anonymous@undisclosed.example.com (Anonymous)</author>
            <pubDate>Tue, 15 Jun 2021 20:09:17 +0000</pubDate>
        </item>
    </channel>
</rss>
