Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3342224
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:48:49+00:00 2026-05-18T00:48:49+00:00

Non-POD derived class PayloadMessage contains an array data member (_payload) whose elements appear to

  • 0

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.

#include <iostream>
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 < 3000; i++) { 
    std::cout << ' ' <<  static_cast<int>(m->_payload[i]); // all zeros, not what I want
  }
}

EDIT: 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?)

To performance test, I changed main() to the following:

int main(int argc, char* argv[]) {
  PayloadMessage* m = new PayloadMessage;
  size_t i = 0;
  for(; i < 1400000; i++) {  // allocates just under 4GB, for 32-bit compatibility
    m = new PayloadMessage;
  }
}

Then compiled and ran a timed test:

$ g++ -O3 test.cpp
$ time ./a.out
real    0m7.068s
user    0m1.393s
sys     0m4.730s

I then added a constructor to do explicit value initialization for _payload and ran the test again:

PayloadMessage() : _payload() {}

$ g++ -O3 test.cpp
$ time ./a.out
real    0m10.361s
user    0m3.582s
sys     0m5.797s

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T00:48:49+00:00Added an answer on May 18, 2026 at 12:48 am

    POD is only explicitly value initialized when using () (8.5 in the C++ standard), otherwise, it is default initialized. Default initialization of POD means it does not do anything to the memory. If operator new is returning zero initialized memory, then the constructor of PayloadMessage isn’t doing any extra work. Out of curiosity, did you inspect the disassembly of PayloadMessage‘s constructor to determine if it’s actually doing anything expensive?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I get this warning from GCC: warning: cannot pass objects of non-POD type 'class
The non-virtual interface idiom describes how the virtual methods are nonpublic customisation points, and
A non-believer of C# was asking me what the purpose to extension methods was.
Speaking as a non-C# savvy programmer, I'm curious as to the evaluation semantics of
I have a non-.Net executable file that is included in my .net assembly as
Should be available to non-U.S. companies, easy to setup, reliable, cheap, customizable, etc. What
I'm calling a non-.NET dll from my project using P/Invoke, meaning that the .dll
How can you strip non-ASCII characters from a string? (in C#)
Suppose I have a non-recurring event that needs to be raised X seconds from
I'm developing non-interactive cpu-bound application which does only computations, almost no IO. Currently it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.