Three patterns of new C + + dynamic memory opening

Source: Internet
Author: User


1.new operator and operator new


New New operator

Delete delete operator

operator new operator NEW

operator delete operator delete

void Main ()

{

date *p_date = (date *)operator new (sizeof (date)); //Just open up space

New (p_date) Date      (1, 1, 1); //Positioning new

P_date->~date (); //The display call of the destructor

operator Delete      (p_date); //release of Space


//equivalent to

date *p_date = new date(1,1,1);

Delete p_date;



//Note: This will cause a memory leak

void* p_date = new date (1, 1, 1);

operator Delete ( p_date       ); (equivalent to free)

//new opened the space and called the constructor, but at the time of the destruction, the system only analyzed the space pointed to by P_date, and did not call the destructor

//Because p_date is a pointer of type void, the system does not know that it is an object, so no destructor is called

}


2. positioning new//placement new


New (primitive pointer) type (initialization of the parameter list);

Note: Constructs an object of a class in the position indicated by the pointer p in the original space


Example: New ( P ) Date (ten);


void * operator new (size_t sz, Date* d , int pos) //Locating the new function (overloaded)

{

return &d [pos];

}

void * operator new (size_t sz,int*d,int POS)

{

return &d [pos];

}

void Main ()

{

date *p = new date;

New (p) Date (1995, 1, 31);

date *p = new date[10];

New (p,2) Date        (1995, 1, 31); //Position new application, initialize the second element

int a[10];

New (A, 5) int (10);

}


3. Three new instances:


#include <iostream>

#include <string>

using namespace std;


void * operator new (size_t n) //operator new

{

return malloc (n );

}

void operator delete (void *p) //operator Delete

{

Free ( p );

}

void * operator new[] (size_t n) //operator new[]

{

return malloc (n );

}

void operator delete[] (void *p) //operator delete[]

{

Free ( p );

}

class Date

{

Public:

Date ( int x = 0): _date (x)

{

}

~date ()

{}

protected :

int _date;

};


Simulation implementation of new, delete

void Test ()

{

date *p = (date *) operator new (sizeof (date)); //Open Space

for (int i = 0; i < 5; i++)

{

New (P + i) Date        (I*10); //placement New (positioning new) where to hit!!!

}

for (int i = 0; i < 5; i++)

{

(P + i)->~date (); //Show off destructor

}

operator Delete        (p); //Free space

}

void Main ()

{

Test ();

}


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="/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"/>


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

Three patterns of new C + + dynamic memory opening

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.