title: C++ Zero-Initialization
body: <p>I'm having trouble understanding when and why exactly a member in my class is zero-initialized according to <a href="http://en.cppreference.com/w/cpp/language/zero_initialization" rel="noreferrer">http://en.cppreference.com/w/cpp/language/zero_initialization</a>.</p>
  
  <p>Consider the following test program:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;stdio.h&gt;
  
  class MyTest {
  private:
      const static unsigned int dimension = 8;
      void (* myFunctions [dimension])();
  
  public: 
      MyTest() {}
  
      void print() { 
          for(unsigned int i=0; i &lt; MyTest::dimension; i++) {
              printf("myFunctions[%d] = %p\n", i, this-&gt;myFunctions[i]);
          }   
      }
  };
  
  
  int main() {
      //We declare and initialize an object on the stack 
      MyTest testObj = {};
      testObj.print();
  
      return 0;
  }
  </code></pre>
  
  <p>I am declaring a class to have an array of 8 function pointers of the signature "void functionname()". When I declare and initialize an object of the class in <code>main</code> as <code>MyTest testObj = {};</code> or <code>MyTest testObj;</code>, I expected it to be zero-initialized, i.e. all pointers are null pointers.</p>
  
  <p>However, compiling with g++ 5.3.0 on my Windows 10 machine with <code>g++ -m32 -o test -std=c++14 test.cpp &amp;&amp; test</code> machine gives the output:</p>
  
  <pre><code>myFunctions[0] = 76dd6b7d
  myFunctions[1] = 00401950
  myFunctions[2] = 0061ff94
  myFunctions[3] = 004019ab
  myFunctions[4] = 00401950
  myFunctions[5] = 00000000
  myFunctions[6] = 003cf000
  myFunctions[7] = 00400080
  </code></pre>
  
  <p>Which look like un-initialized values from the stack.. </p>
  
  <p>If I move the declaration of the object outside of main (as a global variable), it prints all zeroes again. </p>
  
  <p>If I have understood cppreference correctly, this is because I have a variariable with static storage duration, and is thus zero-initialized. It initializes my class type by zero-initializing all non-static data members of my class (i.e., the <code>myFunctions</code>) array. An array is initialized by zero-initializing every element of it, which, in my function pointer case, is a null pointer.</p>
  
  <p>Why does it not zero-initialize my object the stack when I declare it with <code>MyTest testObj = {};</code>? </p>