#include
#include
#include
using namespace std;
//void test()
//{
// int array[10];
// int array2[10] = { 1,2,3 };
// int array3[10] = { 0 };
//}
//void test1()
//{
// //malloc Ö»½øÐпռäÉêÇ룬²»½øÐгõʼ»¯
// int* ptr = (int*)malloc(sizeof(int));
// //calloc ½øÐпռäÉêÇë+Áã³õʼ»¯
// int* ptr2 = (int*)calloc(1, sizeof(int));
// //realloc µÚÒ»¸ö²ÎÊýΪnullptr/NULL£¬¹¦ÄܵȼÛÓÚmalloc
// int* ptr3 = (int*)realloc(nullptr, sizeof(int));
// //µ÷Õû¿Õ¼ä´óС
// //1.Ö±½ÓԵص÷Õû´óС
// //2.ÖØпª¿Õ¼ä ÊÍ·ÅÔÓпռ䣬ÖØÐÂÉêÇë¿Õ¼ä£¬ÄÚÈÝ¿½±´
// 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);
// //µ¥¸öÀàÐ͵Ŀռ䣺new+ÀàÐÍ
// //Á¬Ðø¿Õ¼ä£ºnew+ÀàÐÍ(¸öÊý)
// //µ¥¸öÀàÐÍ¿Õ¼äÉêÇë+³õʼ»¯£ºnew+ÀàÐÍ(³õʼֵ)
// //»ù±¾ÀàÐÍÓÃnewÉêÇëÁ¬Ðø¿Õ¼ä£¬²»Äܳõʼ»¯
// int* ptr3 = new int;
// int* ptr4 = new int[10];
// int* ptr5 = new int[5];
//
// //ÊÍ·Å¿Õ¼ä
// //µ¥¸ö¿Õ¼ä£ºdelete Ö¸Õë
// //Á¬Ðø¿Õ¼ä£ºdelete[] Ö¸Õë
// //ÉêÇëºÍÊͷŵIJÙ×÷Æ¥ÅäʹÓãºmalloc free£¬new delete£¬new[] delete[]
// delete ptr3;
// delete ptr5;
// delete[] ptr4;
//}
//
//void test4()
//{
// //¶¯Ì¬´´½¨×Ô¶¨ÒåÀàÐ͵ĶÔÏó
// //new£º¶¯Ì¬¿ª¿Õ¼ä+µ÷Óù¹Ô캯Êý³õʼ»¯
// //ÉêÇëµ¥¸ö¿Õ¼ä£ºnew ×Ô¶¨ÒåÀàÐÍ(²ÎÊýÁбí)
// Date* pd = new Date(2020);
// Date* pd2 = new Date(2030);
// Date* pd4 = new Date;//µ÷ÓÃĬÈϹ¹Ô죺Î޲Πȫȱʡ
// //ÉêÇëÁ¬ÐøµÄ¿Õ¼ä£¬new×Ô¶¨ÒåÀàÐÍ(¸öÊý)£¬×Ô¶¯µ÷ÓÃĬÈϹ¹Ôì½øÐгõʼ»¯£¬Èç¹ûûÓÐĬÈϹ¹Ô죬±àÒëÆ÷±¨´í
// Date* pd3 = new Date[10];
// //ÊÍ·Å×Ô¶¨ÒåÀàÐ͵Ŀռä
// //delete:µ÷ÓÃÎö¹¹º¯ÊýÇåÀí×ÊÔ´+ÊÍ·Å¿Õ¼ä
// delete pd;
// delete pd2;
// delete pd4;
//
// delete[] pd3;
//}
//
//void test5()
//{
// //void* operator new(size_t n);//²»ÊÇÔËËã·ûÖØÔغ¯Êý ÊÇÈ«¾Öº¯Êý
// // ʹÓ÷½Ê½ºÍmallocÀàËÆ
// // ·â×°malloc + Òì³£
// //new 10;
// //new µÄÖ´Ðйý³Ì(×Ô¶¨ÒåÀàÐÍ) operator new --> malloc --> ¹¹Ô캯Êý
// int* ptr = (int*)operator new(sizeof(int)0x3fffffff);
// int ptr2 = (int*)malloc(sizeof(int));
//
// //void operator delete(void* ptr);//ÀàËÆÓÚfree
// // ʹÓ÷½Ê½ºÍfreeÀàËÆ
// // £º·â×°free
// // deleteÖ´Ðйý³Ì(×Ô¶¨ÒåÀàÐÍ)£ºÎö¹¹–> 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)
{
//ÄÚ´æ³ØÉêÇë
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;
};
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¶¨Î»±í´ïʽ£º new(µØÖ·)ÀàÐÍ(²ÎÊýÁбí)
//ÔÚÒѾ¿ªºÃµÄ¿Õ¼äÉÏÏÔʽµ÷Óù¹Ô캯Êý
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)
// {
//
// }*/
//}
//·ºÐͱà³Ì²¢Ã»ÓмõÉÙʵ¼ÊµÄ´úÂëÁ¿£¬Ö»ÊÇ°ÑÖظ´µÄ´úÂë½»¸ø»úÆ÷×Ô¶¯Éú³É
//¼õÉÙ¿ª·¢ÈËÔ±µÄÖظ´µÄ¹¤×÷Á¿£¬Ìá¸ß¹¤×÷ЧÂÊ
//template
//º¯Êý¶¨Òå
//º¯ÊýÄ£°å
//Ä£°åʵÀý»¯£ºÓÃʵ¼Ê²ÎÊýÀàÐÍ Éú³É¿ÉÖ´Ðеĺ¯Êý
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’;
//ÒþʽʵÀý»¯£º±àÒëÆ÷¸ü¼Ó²ÎÊý½øÐÐ×Ô¶¯ÍƵ¼£¬²úÉúÖ±½Ó¿ÉÖ´ÐеĴúÂë
Swap(a, b);
Swap(c, d);
Date d1(2020);
Date d2(2030);
Swap(d1, d2);
//ÏÔʽʵÀý»¯ º¯ÊýÃû+<ÀàÐÍ>+(²ÎÊýÁбí)
Swap(a, b);
}
int main()
{
//test();
//test8();
return 0;
}