Ray's playground

 

Dynamic Object Creation(Chapter 13 of Thinking in C++)

code
 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <iostream>
 4 using namespace std;
 5 
 6 void* operator new(size_t sz)
 7 {
 8     printf("operator new: %d Bytes\n", sz);
 9     void* m = malloc(sz);
10     if(!m)
11     {
12         puts("out of memory");
13     }
14 
15     return m;
16 }
17 
18 void operator delete(void* m)
19 {
20     puts("operator delete");
21     free(m);
22 }
23 
24 class S
25 {
26     int i[100];
27 public:
28     S()
29     {
30         puts("S::S)");
31     }
32 
33     ~S()
34     {
35         puts("S::~S()");
36     }
37 };
38 
39 int main()
40 {
41     puts("creating & destroying and int");
42     int* p = new int(34);
43     delete p;
44     puts("creating & destroying an s");
45     S* s = new S;
46     delete s;
47     puts("creating & destroying S[3]");
48     S* sa = new S[3];
49     delete[] sa;
50     cin.get();
51 }

 

posted on 2010-11-29 22:33  Ray Z  阅读(182)  评论(0编辑  收藏  举报

导航