(5.3.4)
新的表达式:
:: opt_ new new-placement_ opt new-type-id new-initializeropt
:: opt_ new new-placement_ opt(type-id)new-initializeropt
由new-expression创建的实体具有动态存储持续时间 (3.7.4).[注意:此类实体的生命周期不一定限于创建它的范围. - 结束说明]
我认为以下有1个主要对象(local_object)具有自动存储持续时间,3个虚拟类具有动态存储持续时间.
struct dummy
{
int a;
};
char local_object[256];
dummy * a = new(&local_object) dummy;
dummy * b = new(&local_object +100) dummy;
dummy * c = new(&local_object +200) dummy;
Run Code Online (Sandbox Code Playgroud)
用户@MM认为只有一个对象(local_object),其余的只是指针.它是否正确?
(3.7)
该动态存储持续时间是与创建对象相关联的operator new
我来描述一下我的问题.我已经分析了使用Visual Studio 2015 [Release]生成的以下代码和OllyDbg,并且memset被调用两次(一次使用408字节,后一次使用400字节).很明显,408字节是编译器在我的类上进行零初始化.
为什么会这样?
我的mainclass已经有一个构造函数,它应该摆脱编译器生成的构造函数.
有趣的是:如果我从主类中删除成员"someotherclas sb",m_buffer将只设置一次(由我,400字节).
// ConsoleApplication.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
class someotherclas
{
public:
int * ptr;
};
class mainclass
{
public:
char m_buffer[400];
someotherclas sb; //If I remove this, buffer will be memset once only (by me, in the constructor)
int a;
mainclass()
{
memset(m_buffer, 0x00, sizeof(m_buffer));
a = 6;
}
};
int main(int argc, char * arr[])
{
mainclass * buffer2 = new mainclass;
return 0; …Run Code Online (Sandbox Code Playgroud)