<story><title>Initialization in C++ is Seriously Bonkers</title><url>https://mikelui.io/2019/01/03/seriously-bonkers.html</url></story><parent_chain></parent_chain><comment><author>ajross</author><text>&amp;gt; [Why is a global variable zero?] Because i has static storage duration, it’s initialized to unsigned zero. Why, you ask? Because the standard says so.&lt;p&gt;No, it&amp;#x27;s because i lives in an area allocated at the OS level, and OS has to have initialized that memory with &lt;i&gt;something&lt;/i&gt; (otherwise you&amp;#x27;d have a security bug).  The .bss segment has been zeroed, for obvious reasons, since the advent of modern linkage.&lt;p&gt;The explanation is backwards.  The reason that stack variables are &lt;i&gt;UN&lt;/i&gt;initialized is (contra the article which thinks it&amp;#x27;s because the programmer didn&amp;#x27;t put an initializer in the source code) that memory is on the stack, which is allocated internal to your program and in practice was used previously by some other call for some other purpose.&lt;p&gt;The fact that stack variables are uninitialized by default is actually an intentional feature, not a bug, as it correctly expresses the behavior of the runtime environment.  Now, it may not be a &lt;i&gt;good&lt;/i&gt; feature, and modern language designs have omitted it (by, let&amp;#x27;s remember, relying on much better modern compilers to elide all the extra code that would otherwise have been needed to zero out a stack frame!).&lt;p&gt;But it&amp;#x27;s got nothing to do with the syntax of the language.  You can rely on .bss being zero in assembly, and similarly be surprised that stack memory is going to have junk in it.</text></comment>
<story><title>Initialization in C++ is Seriously Bonkers</title><url>https://mikelui.io/2019/01/03/seriously-bonkers.html</url></story><parent_chain></parent_chain><comment><author>tines</author><text>In my view this author falls into the camp of people calling C++ a mess because it gives them too much control when they ask for it. One of his examples which he calls &amp;quot;The Abyss&amp;quot;:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    #include &amp;lt;iostream&amp;gt;
    struct A {
        A(std::initializer_list&amp;lt;int&amp;gt; l) : i(2) {}
        A(int i = 1) : i(i) {}
        int i;
    };
    int main() {
        A a1;
        A a2{};
        A a3(3);
        A a4 = {5};
        A a5{4, 3, 2};
        std::cout &amp;lt;&amp;lt; a1.i &amp;lt;&amp;lt; &amp;quot; &amp;quot;
                  &amp;lt;&amp;lt; a2.i &amp;lt;&amp;lt; &amp;quot; &amp;quot;
                  &amp;lt;&amp;lt; a3.i &amp;lt;&amp;lt; &amp;quot; &amp;quot;
                  &amp;lt;&amp;lt; a4.i &amp;lt;&amp;lt; &amp;quot; &amp;quot;
                  &amp;lt;&amp;lt; a5.i &amp;lt;&amp;lt; std::endl;
    }
    &lt;/code&gt;&lt;/pre&gt;
which outputs:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    1 1 3 2 2
&lt;/code&gt;&lt;/pre&gt;
he claims to be mysterious but is actually pretty reasonable.&lt;p&gt;In the case of:&lt;p&gt;a1: There is no initializer list in the variable declaration, so ctor 2 is called.&lt;p&gt;a2: An empty initializer list should reasonably behave like a default constructor, and a default constructor should be more efficient than processing an initializer_list, so ctor 2 is called. In general, the constructor with the matching number of arguments and correct types is preferred over initializer_list constructors. Makes sense, being able to specialize on number and type of arguments is more powerful than a design where a single initializer_list ctor invalidates all other ctors.&lt;p&gt;a3: No list, ctor 2 is called.&lt;p&gt;a4: Non-empty init list and no specialized non-init-list ctor, so ctor 1 is called.&lt;p&gt;a5: Several-variable init list and no overriding 3-variable constructor with matching types, therefore ctor 1 is called.&lt;p&gt;Complex? Maybe, but that&amp;#x27;s what you get with C++: very fine control of program semantics, benefit being expressive libraries. No other language fills this niche that I&amp;#x27;m aware of.&lt;p&gt;I agree that C++ isn&amp;#x27;t a good language to teach in a CS 101 class. But no other single language is good either. The goal of CS 101 is to not make students give up before they get hooked, and that can happen because the material is too challenging or not challenging enough. For people in the former category, give them a scripting language and visual feedback, like Lua + Garrysmod. For the latter, give them assembly, haskell, C, C++ (teaching it like &amp;quot;C with templates&amp;quot; not &amp;quot;C with classes&amp;quot;).</text></comment>