new A和new A()的区别详解

我们在C++程序中经常看到两种new的使用方式:new A以及new A()。那么这两种究竟有什么区别呢?


调用new分配的内存有时候会被初始化,而有时候不会,这依赖于A的类型是否是POD(Plain old data)类型,或者它是否是包含POD成员、使用编译器生成默认构造函数的类。


附:POD类型

POD是Plain old data的缩写,它是一个struct或者类,且不包含构造函数、析构函数以及虚函数。

维基百科给出了更加详细的解释:

C++的POD类型或者是一个标量值,或者是一个POD类型的类。POD class没有用户定义的析构函数、拷贝构造函数和非静态的非POD类型的数据成员。而且,POD class必须是一个aggregate,没有用户定义的构造函数,没有私有的或者保护的非静态数据,没有基类或虚函数。它只是一些字段值的集合,没有使用任何封装以及多态特性。


附:aggregate的定义:

An aggregate is an array or a class (clause 9) with no user-declared constructors (12.1), no private or protected non-static data members (clause 11), no base classes (clause 10), and no virtual functions (10.3).


接着介绍一下C++中的三种初始化方式:

zero-initialization,default-initialization,value-initialization

首先需要注意的是value-initialization是在C++2003标准中新引入的,在原来的1998标准中并不存在。

C++03标准中针对这三种方式的说明:

To zero-initialize an object of type T means:
— if T is a scalar type (3.9), the object is set to the value of 0 (zero) converted to T;
— if T is a non-union class type, each nonstatic data member and each base-class subobject is zero-initialized;
— if T is a union type, the object’s first named data member is zero-initialized;
— if T is an array type, each element is zero-initialized;
— if T is a reference type, no initialization is performed.


To default-initialize an object of type T means:
— if T is a non-POD class type (clause 9), the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);
— if T is an array type, each element is default-initialized;
— otherwise, the object is zero-initialized.


To value-initialize an object of type T means:
— if T is a class type (clause 9) with a user-declared constructor (12.1), then the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);
— if T is a non-union class type without a user-declared constructor, then every non-static data member and base-class component of T is value-initialized;
— if T is an array type, then each element is value-initialized;
— otherwise, the object is zero-initialized


A program that calls for default-initialization or value-initialization of an entity of reference type is ill-formed. If T is a cv-qualified type, the cv-unqualified version of T is used for these definitions of zero-initialization, default-initialization, and value-initialization.


注意:VS2008遵循的是98标准,而GCC3.4.5遵循的是03标准。

采用如下代码可以验证编译器遵循的到底是哪一种标准:

  1. #include <stdio.h>  
  2. #include <string.h>  
  3. #include <new>  
  4.   
  5. struct A { int m; }; // POD  
  6. struct B { ~B(); int m; }; // non-POD, compiler generated default ctor  
  7. struct C { C() : m() {}; ~C(); int m; }; // non-POD, default-initialising m  
  8.   
  9. int main()  
  10. {  
  11.     char buf[sizeof(B)];  
  12.     memset( buf, 0x5a, sizeof( buf));  
  13.   
  14.     // use placement new on the memset'ed buffer to make sure   
  15.     //  if we see a zero result it's due to an explicit   
  16.     //  value initialization  
  17.     B* pB = new(buf) B();   //C++98 rules - pB->m is uninitialized  
  18.                             //C++03 rules - pB->m is set to 0  
  19.     printf( "m  is %d\n", pB->m);  
  20.     return 0;  
  21. }  
在VS008中输出就不是0,说明遵循的是98标准。



下面先看一段C++示例代码:

  1. #include <iostream>  
  2.   
  3. using namespace std;  
  4.   
  5. struct A { int m; }; // POD  
  6. struct B { ~B(){}; int m; }; // non-POD, compiler generated default ctor  
  7. struct C { C() : m() {}; ~C(){}; int m; }; // non-POD, default-initialising m  
  8.   
  9. int main()  
  10. {  
  11.     A *aObj1 = new A;  
  12.     A *aObj2 = new A();  
  13.     cout << aObj1->m << endl;  
  14.     cout << aObj2->m << endl;  
  15.   
  16.     B *bObj1 = new B;  
  17.     B *bObj2 = new B();  
  18.     cout << bObj1->m << endl;  
  19.     cout << bObj2->m << endl;  
  20.   
  21.     C *cObj1 = new C;  
  22.     C *cObj2 = new C();  
  23.     cout << cObj1->m << endl;  
  24.     cout << cObj2->m << endl;  
  25.   
  26.     delete aObj1;  
  27.     delete aObj2;  
  28.     delete bObj1;  
  29.     delete bObj2;  
  30.     delete cObj1;  
  31.     delete cObj2;  
  32.   
  33.     return 0;  
  34. }  
运行结果:



上述测试平台是VS2008.需要注意的是,VS08只支持C++98。

在这种情况下:

new A:不确定的值

new A():zero-initialize

new B:默认构造(B::m未被初始化)

new B():默认构造(B::m未被初始化)

new C:默认构造(C::m被zero-initialize)

new C():默认构造(C::m被zero-initialize)


如果用兼容C++03的编译器,比如G++结果:


new A:不确定的值

new A():value-initialize A,由于是POD类型所以是zero initialization

new B:默认构造(B::m未被初始化)

new B():value-initialize B,zero-initialize所有字段,因为使用的默认构造函数

new C:default-initialize C,调用默认构造函数

new C():value-initialize C,调用默认构造函数


在所有C++版本中,只有当A是POD类型的时候,new A和new A()才会有区别。而且,C++98和C++03会有区别。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值