title: Can I prevent zero initialization of the elements in an array data member in a non-POD class?
body: <p>Non-POD derived class PayloadMessage contains an array data member (_payload) whose elements appear to be getting zero initialized upon construction.  I don't want this to happen for performance/efficiency reasons -- it is a large array.  Suggestions?  (Placement new maybe?)  I'm using an older g++ compiler, 3.4.6.</p>
  
  <pre><code>#include &lt;iostream&gt;
  class BaseMessage {
  public:
    enum CCC_MessageType {  START_THREAD, KILL_THREAD };
    CCC_MessageType _type;
    virtual ~BaseMessage() {}  // class has other non-POD class stuff
  };
  class PayloadMessage : public virtual BaseMessage {
  public:
    uint16_t _payload_length;
    uint8_t  _payload[3000];
  };
  int main(int argc, char* argv[]) {
    PayloadMessage* m = new PayloadMessage;
    size_t i = 0;
    for(; i &lt; 3000; i++) { 
      std::cout &lt;&lt; ' ' &lt;&lt;  static_cast&lt;int&gt;(m-&gt;_payload[i]); // all zeros, not what I want
    }
  }
  </code></pre>
  
  <p><strong>EDIT:</strong> Okay, based on comments/answers, and some testing (shown below), the array is not being initialized.  (Does anyone know what might be causing the memory to appear to be zero-ed out?)</p>
  
  <p>To performance test, I changed main() to the following:</p>
  
  <pre><code>int main(int argc, char* argv[]) {
    PayloadMessage* m = new PayloadMessage;
    size_t i = 0;
    for(; i &lt; 1400000; i++) {  // allocates just under 4GB, for 32-bit compatibility
      m = new PayloadMessage;
    }
  }
  </code></pre>
  
  <p>Then compiled and ran a timed test:</p>
  
  <pre><code>$ g++ -O3 test.cpp
  $ time ./a.out
  real    0m7.068s
  user    0m1.393s
  sys     0m4.730s
  </code></pre>
  
  <p>I then added a constructor to do explicit value initialization for _payload and ran the test again:</p>
  
  <pre><code>PayloadMessage() : _payload() {}
  
  $ g++ -O3 test.cpp
  $ time ./a.out
  real    0m10.361s
  user    0m3.582s
  sys     0m5.797s
  </code></pre>
  
  <p>Yep, the second version with the explicit initialization takes longer, so I assume the first version is not doing initialization (it just looks that way).  Thanks all for your help.</p>