??????????????????new??????
1 void *__CRTDECL operator new(size_t size) _THROW1(_STD bad_alloc)
2         {       // try to allocate size bytes
3         void *p;
4         while ((p = malloc(size)) == 0)
5                 if (_callnewh(size) == 0)
6                 {       // report no memory
7                 static const std::bad_alloc nomem;
8                 _RAISE(nomem);
9                 }
10
11         return (p);
12         }
???????????????????????malloc??????????????Щ??????????????new??malloc????????????????
??????????????????????????????????????????????new??????????g++??????????????δ????????
????2. New?????
???????????????new????“new operator”??“new expression”?????????C++??????new??????????????????????壺new operator??operator new??placement new??
???????new operator??    ??????????new?????????????????????????????????????????????????
???????operator new??    ????????????????new operator???????????operator new??????????new?????????????????????????????????÷????????????????????????????????????????????new_handler??
???????????????operator new???????????????????????????operator new???????????????????????????????????????A???????壺
1 class A {
2     int m_value;
3 public:
4     A(int value) :m_value(value * value){}
5
6     void Func(){
7         printf("m_value=%d "?? m_value);
8     }
9
10     void* operator new(size_t size){
11         printf("operator new called ");
12         return ::operator new(size);
13     }
14 };
?????????? “A *a = new A(1);” ????????????Console??????????"operator new called"???????operator new???????????????????????????????????????Щ???????????£?????????????
????????   "::operator new(size)" ??仰???????????????operator new??????????????????????????????????
??????????????????operator new?????????????????operator new???????????????????????????????????????????operator new?????????????????????????????????????????????????′???
????1 void* operator new(size_t size)
????2 {
????3   printf("global new ");
????4   return malloc(size);
????5 }
?????????д??? “A *a = new A(1);” ??????????????"global new"???????????????operator new??????????????????????????ó?????????????????????????????
?????????????????“??malloc???????????????????÷???????????????new_handler”?????????????????new_handler????????????????????????????????????????????????????????????????????????
1 #include <iostream>
2 using namespace std;
3
4 class A {
5     int m_value;
6     static new_handler    current_handler;
7 public:
8     static new_handler set_new_handler(new_handler p){
9         new_handler old_handler = current_handler; //???洫???Handler???????????Handler
10         current_handler = p;
11         return old_handler;
12     }
13
14     void* operator new(size_t size){
15         new_handler globe_handler = std::set_new_handler(current_handler); // ??????????set_new_handler????handler
16
17         void *memory = NULL;
18         try{
19             memory = ::operator new(size);
20         }catch(std::bad_alloc){
21             std::set_new_handler(globe_handler);
22             throw;
23         }
24         std::set_new_handler(globe_handler);
25
26         return memory;
27     }
28
29     A(int value) :m_value(value * value){}
30 };
31 new_handler A::current_handler = NULL;
32
33 void handlerFunc(){
34     cout<<"Bad Memory."<<endl;
35 }
36
37 int main()
38 {
39     A::set_new_handler(handlerFunc);
40     A *aPtr = new A(1);
41
42     delete aPtr;
43     system("pause");
44     return 0;
45 }
?????????????????????????????????????????????????е????????????????????????????????????????????????????????????
??????. placement new:  ??????????????????????new?????????????——???ù??????????濴????????÷?????
1 #include <iostream>
2 using namespace std;
3
4 class A {
5     int m_value;
6 public:
7     void* operator new(size_t size){
8         cout<<"operator new."<<endl;
9         void* memory = NULL;
10         memory = malloc(size);
11         return memory;
12     }
13     A(int value) :m_value(value * value){}
14 };
15
16 int main()
17 {
18     A *aPtr = new A(1);
19     ::new(aPtr) A(1);
20
21     delete aPtr;
22     system("pause");
23     return 0;
24 }
??????????????????????placement new????÷?????????ж?????F11??????????????????????£?
????1 inline void *__CRTDECL operator new(size_t?? void *_Where) _THROW0()
????2     {    // construct array with placement at _Where
????3     return (_Where);
????4     }
???????????????operator new?????????????placement new????????????????????С???????????????operator new???????????????????????????????С??
????3. ???
?????????C++??new????????壺new operator??operator new??placement new??
?????????new operator?????????????new??????????????????????????????????楨???ù????????????????
?????????operator new??????????????????????????????????????????????Щ??????
?????????placement new????ù?????????????д?????????“::new(aPtr) A(1)”