New/delete and new[]/delete[] the underlying invocation and simple implementation

Source: Internet
Author: User

There are two things that you do when you use NEW:

1. Call operator new to allocate space

2. Call the constructor to initialize the object


There are two things that you do when you use Delete:

1. Call destructor Cleanup Object

2. Call operator delete function to free up space


Two things were done when using New[n]:

1. Call operator new to allocate space

2. Call N-Times constructor to initialize N objects


Two things were done when using delete[]:

1. Call N-time destructor to clean n objects

2. Call operator delete function to free up space


650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>

650) this.width=650; "Src=" http://img.blog.csdn.net/20160830160733513?watermark/2/text/ Ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity/center "/>


Position new expression: The position new Expression initializes an object by invoking the constructor in the original space of the assignment.

New (place_address) type;

New (place_address) type (initializer-list);

Place_address must be a pointer, initializer-list is a list of initialization


Use Mallco/free and position new expressions to simulate New/delete and new[]/delete[]:

Cases:

Class a{public:a (int a=2): _a (a) {cout << "a ()" << Endl;} void Show () {cout << _a << Endl;} ~a () {cout << "~a ()" << Endl;} Private:int _a;};   void Test () {//allocates 1 A-type space a *pa = (A *) malloc (sizeof (a)); New (PA) a (1);      Call the constructor pa->~a (); Call destructor Free (PA);//Allocate 10 a type of space A * PA = (A *) malloc (sizeof (a) *10); for (int i = 0; i < i++)//Call 10 constructor {new (PA + i) A (i);} for (int i = 0; i < i++)//Call 10 destructors {(PA + i)->~a ();} Free (PA);}



This article from the "11132019" blog, reproduced please contact the author!

New/delete and new[]/delete[] the underlying invocation and simple implementation

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.