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