memory managernent

#include
#include
#include
using namespace std;
//void test()
//{
// int array[10];
// int array2[10] = { 1,2,3 };
// int array3[10] = { 0 };
//}
//void test1()
//{
// //malloc ????DD????éê??£?2???DD3?ê??ˉ
// int* ptr = (int*)malloc(sizeof(int));
// //calloc ??DD????éê??+á?3?ê??ˉ
// int* ptr2 = (int*)calloc(1, sizeof(int));
// //realloc μúò???2?êy?anullptr/NULL£?1|?üμè??óúmalloc
// int* ptr3 = (int*)realloc(nullptr, sizeof(int));
// //μ÷??????′óD?
// //1.?±?ó?-μ?μ÷??′óD?
// //2.??D??a???? êí·??-óD????£???D?éê??????£??úèY??±′
// int* ptr4 = (int*)realloc(ptr, sizeof(int) * 4);
//}
class Date
{
public:
Date(int year=2030)
:_year(year)
{}
~Date()
{
cout << “~Date()” << endl;
}
private:
int _year;
};
//void test2()
//{
// Date d(2020);
// Date* pd = (Date*)malloc(sizeof(Date));
// Date* pd2 = (Date*)calloc(1, sizeof(Date));
//}
//
//void test3()
//{
// int* ptr = (int*)malloc(sizeof(int));
// int* ptr2 = (int*)malloc(sizeof(int) * 10);
// //μ¥??ààDíμ?????£onew+ààDí
// //á?D?????£onew+ààDí(??êy)
// //μ¥??ààDí????éê??+3?ê??ˉ£onew+ààDí(3?ê??μ)
// //?ù±?ààDíó?newéê??á?D?????£?2??ü3?ê??ˉ
// int* ptr3 = new int;
// int* ptr4 = new int[10];
// int* ptr5 = new int[5];
//
// //êí·?????
// //μ¥??????£odelete ????
// //á?D?????£odelete[] ????
// //éê??oíêí·?μ?2ù×÷?¥??ê1ó?£omalloc free£?new delete£?new[] delete[]
// delete ptr3;
// delete ptr5;
// delete[] ptr4;
//}
//
//void test4()
//{
// //?ˉì?′′?¨×??¨ò?ààDíμ????ó
// //new£o?ˉì??a????+μ÷ó?11?ìoˉêy3?ê??ˉ
// //éê??μ¥??????£onew ×??¨ò?ààDí(2?êyáD±í)
// Date* pd = new Date(2020);
// Date* pd2 = new Date(2030);
// Date* pd4 = new Date; //μ÷ó???è?11?ì£o?T2? è?è±ê?
// //éê??á?D?μ?????£?new×??¨ò?ààDí(??êy)£?×??ˉμ÷ó???è?11?ì??DD3?ê??ˉ£?è?1???óD??è?11?ì£?±àò??÷±¨′í
// Date* pd3 = new Date[10];
// //êí·?×??¨ò?ààDíμ?????
// //delete:μ÷ó???11oˉêy??àí×ê?′+êí·?????
// delete pd;
// delete pd2;
// delete pd4;
//
// delete[] pd3;
//}
//
//void test5()
//{
// //void* operator new(size_t n); //2?ê?????·?????oˉêy ê?è???oˉêy
// // ê1ó?·?ê?oímallocàà??
// // ·a×°malloc + òì3£
// //new 10;
// //new μ??′DD1y3ì(×??¨ò?ààDí) operator new --> malloc --> 11?ìoˉêy
// int* ptr = (int*)operator new(sizeof(int)0x3fffffff);
// int
ptr2 = (int*)malloc(sizeof(int));
//
// //void operator delete(void* ptr); //àà??óúfree
// // ê1ó?·?ê?oífreeàà??
// // £o·a×°free
// // delete?′DD1y3ì(×??¨ò?ààDí)£o??11–> operator delete -->free
// operator delete(ptr);
// free(ptr2);
// free(nullptr);
// operator delete(nullptr);
//}
struct Node
{
public:
Node()
{
cout << “Node()” << endl;
}
//?¨???úμ?????μ?éê??·?ê?
void* operator new(size_t n)
{
//?ú′?3?éê??
void* ptr=allocator().allocate(1);
cout << “mem poo; allocate” << endl;
return ptr;
}

void operator delete(void* ptr) { allocator().deallocate((Node*)ptr, 1); cout << "mem pool deallocate" << endl; }

private:
int _data;
Node* _next;
};
【memory managernent】void test6()
{
Date* pd = new Date;
Date* pd2 = new Date[10];
Node* pn = new Node; Node* pn2 = new Node[10]; delete pd; delete[] pd2; delete pn; delete[] pn2;

}
void test7()
{
Date* pd = (Date*)malloc(sizeof(Date));
//new?¨??±í′?ê?£o new(μ??·)ààDí(2?êyáD±í)
//?úò??-?ao?μ?????é???ê?μ÷ó?11?ìoˉêy
new(pd)Date(2030);
Date* pd2 = (Date*)malloc(sizeof(Date));
new(pd2)Date;
}
//
//void test8()
//{
// //char* ptr = new char[0x7fffffff];
// try
// {
// char* ptr = new char[10000];
// char* ptr2 = new char[0x7fffffff];
// cout << “delete” << endl;
// delete[] ptr;
// }
// catch (exception& e)
// {
// cout << e.what() << endl;
// }
// /catch (CFileException e)
// {
//
// }
// catch (CException* e)
// {
//
// }*/
//}
//·oDí±à3ì2¢??óD??éùêμ?êμ?′ú??á?£???ê?°????′μ?′ú???????ú?÷×??ˉéú3é
//??éù?a·¢è??±μ????′μ?1¤×÷á?£?ìá??1¤×÷D§?ê
//template
//oˉêy?¨ò?
//oˉêy?£°?
//?£°?êμày?ˉ£oó?êμ?ê2?êyààDí éú3é?é?′DDμ?oˉêy
template
void Swap(T& left, T& right)
{
T temp = left;
left = right;
right = temp;
}
void test9()
{
int a = 1, b =2;
char c = ‘a’, d = ‘b’;
//òtê?êμày?ˉ£o±àò??÷?ü?ó2?êy??DD×??ˉí?μ?£?2úéú?±?ó?é?′DDμ?′ú??
Swap(a, b);
Swap(c, d);
Date d1(2020);
Date d2(2030);
Swap(d1, d2);
//??ê?êμày?ˉ oˉêy??+<ààDí>+(2?êyáD±í)
Swap(a, b);
}
int main()
{
//test();
//test8();
return 0;
}

    推荐阅读