23.内存池

 1 #include <iostream>
 2 //基本数据类型内存池
 3 #include <boost/pool/pool.hpp>
 4 //类类型内存池
 5 #include <boost/pool/object_pool.hpp>
 6 //单例模式
 7 #include <boost/pool/singleton_pool.hpp>
 8 using namespace std;
 9 using namespace boost;
10 
11 void main1()
12 {
13     //开辟一段内存,五百个字节
14     pool<>p1(500);
15     //开辟内存
16     int *p = (int *)p1.malloc();
17     *p = 4;
18     cout << *p << endl;
19     cin.get();
20 }
21 
22 class myclass
23 {
24 public:
25     myclass()
26     {
27         cout << "hello" << endl;
28     }
29 };
30 
31 
32 void main2()
33 {
34     object_pool<myclass> p1;
35     //需要手动调用构造
36     myclass *pmy = p1.malloc();
37     //调用构造函数
38     pmy = p1.construct();
39     cin.get();
40 }
41 
42 struct pool_tag
43 {
44 
45 };
46 
47 void main()
48 {
49     //单例模式,只能装一个对象
50     typedef singleton_pool<pool_tag, sizeof(int)> spl;
51     int *p = (int *)spl::malloc();
52     //静态函数,释放内存
53     spl::release_memory();
54 
55     cin.get();
56 }

 

posted @ 2018-03-30 15:18  喵小喵~  阅读(111)  评论(0编辑  收藏  举报