是否可以防止非POD类中的数组数据成员中的元素零初始化? [英] Can I prevent zero initialization of the elements in an array data member in a non-POD class?

查看:69
本文介绍了是否可以防止非POD类中的数组数据成员中的元素零初始化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

非POD派生类PayloadMessage包含一个数组数据成员(_payload),其元素在构造时似乎初始化为零。我不希望出于性能/效率方面的原因而发生这种情况-它的阵列很大。有什么建议吗? (可能是新的位置?)我使用的是较旧的g ++编译器3.4.6。

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
  }
}

编辑:好吧,基于注释/答案和一些测试(如下所示),该数组未初始化。 (有人知道是什么原因导致内存似乎被归零吗?)

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

要进行性能测试,我将main()更改为以下内容:

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

然后我添加了一个构造函数来进行显式_payload的值初始化并再次运行测试:

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.

推荐答案

POD仅在使用()(在C ++标准中为8.5),否则默认初始化。 POD的默认初始化意味着它不对内存做任何事情。如果 operator new 返回零的初始化内存,则 PayloadMessage 的构造函数没有做任何额外的工作。出于好奇,您是否检查过 PayloadMessage 的构造函数的反汇编以确定它是否实际上在做任何昂贵的事情?

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?

这篇关于是否可以防止非POD类中的数组数据成员中的元素零初始化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆