新人报到,第一篇
学习C++有一段时间了,最近几个月才有所心得,希望在这里和大家共同分享。
最近利用一点时间写了一个matrix锻炼一下自己,因为学习的时候都会用到,所以自己就随便写写。中间学到不少。
比如,最奇怪的是,在我这个LMatrix<T>模板类里,直接使用setioflags会提示说“setioflags不是模板类,不支持模板参数”。
后来在书上看到,在头文件加上
1
using
std::setioflags
就完全没有问题了。真是不得其解啊!
另外,intel C++ Compiler 11 在编译的时候,没有任何问题,但g++ 4.4.1(在ubuntu 9.10 下)却有这么个提示:
1
template
<
typename T
>
2 const LMatrix < T > ::Matrix2p LMatrix < T > ::lu_resolve() const
说在”<"之前要初始化LMatrix<T>,而Matrix2p只是LMatrix<T>里的一个嵌套类。我测试了这个函数,完全正常,只是g++ 不能编译通过。期待高人解决之。
2 const LMatrix < T > ::Matrix2p LMatrix < T > ::lu_resolve() const
由于intel还不支持右值引用,所以代码里的时髦函数子就只好处理掉了。
看了《efficient c++》,本来想把它的固定大小的内存池拿来直接用,结果发现最后释放内存池的时候,崩溃了。代码如下:
1
template
<
typename T
>
2 class LMatrix
3 {
4 public :
5 //
6 // other functions
7
8 ///////// //overload operator new and delete to use the memorypool /////////////// /
9 inline void * operator new (size_t size)
10 {
11 return memPool -> alloc(size);
12 }
13
14 inline void operator delete( void * element, size_t size)
15 {
16 memPool -> free(element);
17 }
18
19 static void newMemoryPool()
20 {
21 memPool = new ByteMemoryPool;
22 }
23
24 static void deleteMemoryPool()
25 {
26 delete memPool;
27 }
28 ////////////////////////////////////
29
30 private :
31 static ByteMemoryPool * memPool;
32 }
2 class LMatrix
3 {
4 public :
5 //
6 // other functions
7
8 ///////// //overload operator new and delete to use the memorypool /////////////// /
9 inline void * operator new (size_t size)
10 {
11 return memPool -> alloc(size);
12 }
13
14 inline void operator delete( void * element, size_t size)
15 {
16 memPool -> free(element);
17 }
18
19 static void newMemoryPool()
20 {
21 memPool = new ByteMemoryPool;
22 }
23
24 static void deleteMemoryPool()
25 {
26 delete memPool;
27 }
28 ////////////////////////////////////
29
30 private :
31 static ByteMemoryPool * memPool;
32 }
内存池LMemoryPool.hpp的代码原样引自《efficient C++》:
1
#ifndef L_MEMORYPOOL_H_
2 #define L_MEMORYPOOL_H_
3 #include " LMatrix.hpp "
4
5 template < typename T >
6 class LMemoryPool
7 {
8 public :
9 LMemoryPool(size_t size = EXPANSION_SIZE);
10 ~ LMemoryPool();
11 inline void * alloc(size_t size);
12 inline void free( void * element);
13 private :
14 LMemoryPool < T >* next;
15 static const size_t EXPANSION_SIZE = 32 ;
16 void expandList(size_t listLength = EXPANSION_SIZE);
17 };
18
19 template < typename T >
20 LMemoryPool < T > ::LMemoryPool(size_t size)
21 {
22 expandList(size);
23 }
24
25 template < typename T >
26 LMemoryPool < T > :: ~ LMemoryPool()
27 {
28 LMemoryPool < T >* pNext = next;
29 for (pNext = next; pNext != NULL; pNext = next)
30 {
31 next = next -> next;
32 delete[] pNext;
33 }
34 }
35
36 template < typename T >
37 void * LMemoryPool < T > ::alloc(size_t size)
38 {
39 if (next != 0 )
40 expandList();
41 LMemoryPool < T >* head = next;
42 next = head -> next;
43 return head;
44 }
45
46 template < typename T >
47 void LMemoryPool < T > ::free( void * element)
48 {
49 LMemoryPool < T >* head = static_cast < LMemoryPool < T >* > (element);
50 head -> next = next;
51 next = head;
52 }
53
54 template < typename T >
55 void LMemoryPool < T > ::expandList(size_t listLength)
56 {
57 size_t size = ( sizeof (T) > sizeof (LMemoryPool < T >* ))
58 ? sizeof (T) : sizeof (LMemoryPool < T >* );
59 LMemoryPool < T >* itr = reinterpret_cast < LMemoryPool < T >* > ( new char [size]);
60
61 next = itr;
62
63 for (size_t i = 0 ; i < listLength; ++ i)
64 {
65 itr -> next = reinterpret_cast < LMemoryPool < T >* > ( new char [size]);
66 itr = itr -> next;
67 }
68 itr -> next = NULL;
69
70 }
71 #endif /*L_MEMORYPOOL_H_ */
2 #define L_MEMORYPOOL_H_
3 #include " LMatrix.hpp "
4
5 template < typename T >
6 class LMemoryPool
7 {
8 public :
9 LMemoryPool(size_t size = EXPANSION_SIZE);
10 ~ LMemoryPool();
11 inline void * alloc(size_t size);
12 inline void free( void * element);
13 private :
14 LMemoryPool < T >* next;
15 static const size_t EXPANSION_SIZE = 32 ;
16 void expandList(size_t listLength = EXPANSION_SIZE);
17 };
18
19 template < typename T >
20 LMemoryPool < T > ::LMemoryPool(size_t size)
21 {
22 expandList(size);
23 }
24
25 template < typename T >
26 LMemoryPool < T > :: ~ LMemoryPool()
27 {
28 LMemoryPool < T >* pNext = next;
29 for (pNext = next; pNext != NULL; pNext = next)
30 {
31 next = next -> next;
32 delete[] pNext;
33 }
34 }
35
36 template < typename T >
37 void * LMemoryPool < T > ::alloc(size_t size)
38 {
39 if (next != 0 )
40 expandList();
41 LMemoryPool < T >* head = next;
42 next = head -> next;
43 return head;
44 }
45
46 template < typename T >
47 void LMemoryPool < T > ::free( void * element)
48 {
49 LMemoryPool < T >* head = static_cast < LMemoryPool < T >* > (element);
50 head -> next = next;
51 next = head;
52 }
53
54 template < typename T >
55 void LMemoryPool < T > ::expandList(size_t listLength)
56 {
57 size_t size = ( sizeof (T) > sizeof (LMemoryPool < T >* ))
58 ? sizeof (T) : sizeof (LMemoryPool < T >* );
59 LMemoryPool < T >* itr = reinterpret_cast < LMemoryPool < T >* > ( new char [size]);
60
61 next = itr;
62
63 for (size_t i = 0 ; i < listLength; ++ i)
64 {
65 itr -> next = reinterpret_cast < LMemoryPool < T >* > ( new char [size]);
66 itr = itr -> next;
67 }
68 itr -> next = NULL;
69
70 }
71 #endif /*L_MEMORYPOOL_H_ */
测试的testMatrix.cpp用例如下:
1
#include
"
LMatrix.hpp
"
2 LMemoryPool < LMatrix < int > >* LMatrix < int > ::memPool = 0 ;
3 int main( )
4 {
5 const int size = 4 ;
6 LMatrix < int >* array[size];
7 LMatrix < int > ::newMemoryPool();
8 for ( int j = 0 ; j < size / 2 ; ++ j)
9 {
10 for ( int i = 0 ; i < size; ++ i)
11 array[i] = new LMatrix < int > ( 8 , 8 );
12
13 for ( int k = 0 ; k < size; ++ k)
14 delete array[k];
15 }
16
17 LMatrix < int > ::deleteMemoryPool();
18
19 return 0 ;
20 }
21
一切正常直到上面测试程序的第17行,追踪到内存池LMemoryPool.hpp的析构函数(第32行),发现它竟然被多次执行了。这不是很奇怪吗?
2 LMemoryPool < LMatrix < int > >* LMatrix < int > ::memPool = 0 ;
3 int main( )
4 {
5 const int size = 4 ;
6 LMatrix < int >* array[size];
7 LMatrix < int > ::newMemoryPool();
8 for ( int j = 0 ; j < size / 2 ; ++ j)
9 {
10 for ( int i = 0 ; i < size; ++ i)
11 array[i] = new LMatrix < int > ( 8 , 8 );
12
13 for ( int k = 0 ; k < size; ++ k)
14 delete array[k];
15 }
16
17 LMatrix < int > ::deleteMemoryPool();
18
19 return 0 ;
20 }
21
类的静态成员是全局共享的,这里只有一次调用,为什么会造成多次析构呢?期待高人。