C++new and New () Differences (Learn)

Source: Internet
Author: User
Tags scalar

We often see two new ways to use it in C + + programs:NewA and new A (). So what is the difference between these two? The memory that the call to new allocates is sometimes initialized, and sometimes not, depending on whether the type of a is a pod (Plain old data) type, or whether it is a class that contains POD members and uses the compiler to generate a default constructor. Attached: Pod type pod is an abbreviation for plain old data, which is a struct or class and does not contain constructors, destructors, and virtual functions. Wikipedia gives a more detailed explanation: C++The pod type is either a scalar value, or a class of pod type. POD class does not have user-defined destructors, copy constructors, and non-static data members for non-pod types. Also, POD class must be a aggregate, no user-defined constructor, no private or protected non-static data, no base class or virtual function. It is just a collection of some field values, with no encapsulation and polymorphic properties. Attached: aggregate definition: an aggregate isAn array or aclass(clause9) with no user-declared constructors (12.1), noPrivateOrprotectednon-StaticData members (clause One), noBaseClasses (clauseTen), and NoVirtualFunctions (10.3). Then introduce the C++three ways to initialize in: zero-initialization,default-initialization,value-initialization. The first thing to note is the value-initialization is in C + +The newly introduced in the 2003 standard does not exist in the original 1998 standard. C++03 Instructions for these three ways in the standard: to zero-initialize anObjectof type T means:-ifT isA scalar type (3.9), theObject  is SetTo the value of0(zero) converted to t;-ifT isA non-unionclassType, each nonstatic data member and eachBase-classSubobject iszero-initialized;-ifT isA union type, theObject' s first named data member iszero-initialized;-ifT isAn array of type, each element iszero-initialized;-ifT isA reference type, no initialization isperformed. todefault-initialize anObjectof type T means:-ifT isA non-podclassType (clause9), thedefaultConstructor forT isCalled (and the initialization isIll-formedifT has no accessibledefaultconstructor); -ifT isAn array of type, each element is default-Initialized;-otherwise, theObject  iszero-initialized. To value-initialize anObjectof type T means:-ifT isAclassType (clause9) with a user-declared constructor (12.1), then thedefaultConstructor forT isCalled (and the initialization isIll-formedifT has no accessibledefaultconstructor); -ifT isA non-unionclassType without a user-declared constructor, then every non-Staticdata member andBase-classComponent of T isvalue-initialized;-ifT isAn array of type, then each element isvalue-Initialized;-otherwise, theObject  iszero-initialized A program that calls for default-initialization or value-initialization of an entity of reference type isIll-formed. If T isA cv-qualified type, the cv-unqualified version of T isUsed forThese definitions of zero-initialization,default-initialization, and value-initialization. Note: VS2008 follows the 98 standard, while the GCC3.4. 5 followed by the 03 standard. Use the following code to verify which criteria the compiler follows: #include<stdio.h>#include<string.h>#include<New>structAintM };//PODstructB {~b ();intM };//non-pod, compiler generated default ctorstructc {C (): M () {}; ~c ();intM };//non-pod, default-initialising mintMain () {Charbuf[sizeof(B)]; memset (BUF,0x5a,sizeof(BUF)); //Use placement New in the Memset ' Ed buffer to make sure//if we see a zero result it's due to an explicit//Value Initializationb* PB =New(BUF) B ();//c++98 rules-pb->m is uninitialized//c++03 Rules-pb->m is set to 0printf"m is%d\n", pb->m); return 0;} The output in VS008 is not 0, which indicates that the 98 standard is followed. Let's take a look at C++example code: #include<iostream>using namespacestd;structAintM };//PODstructB {~b () {};intM };//non-pod, compiler generated default ctorstructc {C (): M () {}; ~c () {};intM };//non-pod, default-initialising mintMain () {A*aobj1 =NewA; A*aobj2 =NewA (); cout<< aobj1->m <<Endl; cout<< aobj2->m <<Endl; B*bobj1 =NewB; B*bobj2 =NewB (); cout<< bobj1->m <<Endl; cout<< bobj2->m <<Endl; C*cobj1 =NewC; C*cobj2 =NewC (); cout<< cobj1->m <<Endl; cout<< cobj2->m <<Endl;  Delete aObj1;  Delete AObj2;  Delete bObj1;  Delete BObj2;  Delete cObj1;  Delete CObj2; return 0;} Running result: The test platform above is VS2008. Note that the VS08 only supports C++98. In this case:NewA: Indeterminate valueNewA (): zero-InitializeNewB: Default construction (b::m not initialized)NewB (): Default construct (b::m not initialized)NewC: Default construct (C::m is zero-Initialize)NewC (): Default construct (C::m is zero-Initialize) if compatible with C++the 03 compiler should produce the following results:NewA: Indeterminate valueNewA (): value-initialize a, because it is a pod type, so it is zero initializationNewB: Default construction (b::m not initialized)NewB (): Value-initialize b,zero-initialize all fields, because the default constructor usedNewC:default-Initialize C, calling the default constructorNewC (): value-Initialize C, call the default constructor at all C+ + version, only if A is the pod type,NewA and new A () will only make a difference. Also, c++98 and C + +03 will make a difference. Reference: What is POD typesinchC++?What is aggregates and PODs and how/why is they special?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.