是c++ primer中的例子,做了一些修改,但仍然有内存泄露,再找找办法...。
Code
#include "stdafx.h"
#include <vld.h>
#include <iostream>
#include <stdexcept>
#define _DEBUG_
/*
内存分配类,预分配一个对象,并维持一个自由列表
*/
template <typename T> class CachedObj
{
public:
void* operator new (std::size_t);
void operator delete (void*, std::size_t);
virtual ~CachedObj();
protected:
T *next;
//private:
public:
static void add_to_freelist(T*);
static std::allocator<T> alloc_mem;
static void releaseMemory();
static T *freeList;
static const std::size_t chunk;
static size_t count;//分配的内存数量
};
using namespace std;
template <typename T> void* CachedObj<T>::operator new (size_t sz)
{
// new should only be asked to build a T,
// not and object derived from T,
// check that right size is requested
#ifdef _DEBUG_
std::cout << "CachedObj<T>::operator new" << std::endl;
#endif
if (sz != sizeof(T))
throw runtime_error("CachedObj: wrong size object in operator new");
if (!freeList)
{
// ths list is empty: grab a new chunk of memory
// allocates chunk number of objects of type T
T *array = alloc_mem.allocate(chunk);
count += chunk;
// now set the next pointers in each object in the allocated memory
for (size_t i = 0; i != chunk; ++i)
{
cout<<"分配内存"<<i<<endl;
add_to_freelist(&array[i]);
}
}
T *p = freeList;
//为了防止派生类中也有next成员,所以在freeList->CachedObj<T>::next用类名进行限制访问next成员
freeList = freeList->CachedObj<T>::next;
cout<<"从freelist取得指针 p="<<p<<endl;
return p; // constructor of T will construct the T part of the object
}
template <typename T> void CachedObj<T>::operator delete (void *p, size_t)
{
#ifdef _DEBUG_
std::cout << "CachedObj<T>::operator delete" << std::endl;
#endif
if (p != 0)
{ // put the "deleted" object back at head of freelist
add_to_freelist(static_cast<T*>(p));
cout<<"增加到自由链表"<<p<<endl;
}
}
template <typename T> void CachedObj<T>::add_to_freelist (T *p)
{
p->CachedObj<T>::next = freeList;
freeList = p;
}
template <typename T> void CachedObj<T>::releaseMemory ()
{
//size_t i = 0;
//T* p;
////if(count > 0)
////{
//// alloc_mem.deallocate(freeList, count);
//// count = 0;
//// freeList = NULL;
////}
//while(i < count)
//{
// p = freeList;
// freeList = freeList->CachedObj<T>::next;
//
// //alloc_mem.deallocate(freeList, 1);
// free(p);
//}
}
template <typename T> CachedObj<T>::~CachedObj()
{
}
//声明静态的变量和方法
template <typename T> allocator<T> CachedObj<T>::alloc_mem;
template <typename T> T* CachedObj<T>::freeList = 0;
template <typename T> const size_t CachedObj<T>::chunk = 24;
template <typename T> size_t CachedObj<T>::count = 0;
template <typename T>
class Gtest: public CachedObj< Gtest<T> >
{
public:
T t;
Gtest():t(0) { }
};
int main ()
{
Gtest<int> *t = new Gtest<int>;
delete t;
Gtest<float> *f = new Gtest<float>;
delete f;
//可见要对每一个类型进行释放内存
cout<<Gtest<int>::count<<endl;
cout<<Gtest<int>::count<<endl;
//cout<<Gtest<float>::count<<endl;
return 0;
}
#include "stdafx.h"
#include <vld.h>
#include <iostream>
#include <stdexcept>
#define _DEBUG_
/*
内存分配类,预分配一个对象,并维持一个自由列表
*/
template <typename T> class CachedObj
{
public:
void* operator new (std::size_t);
void operator delete (void*, std::size_t);
virtual ~CachedObj();
protected:
T *next;
//private:
public:
static void add_to_freelist(T*);
static std::allocator<T> alloc_mem;
static void releaseMemory();
static T *freeList;
static const std::size_t chunk;
static size_t count;//分配的内存数量
};
using namespace std;
template <typename T> void* CachedObj<T>::operator new (size_t sz)
{
// new should only be asked to build a T,
// not and object derived from T,
// check that right size is requested
#ifdef _DEBUG_
std::cout << "CachedObj<T>::operator new" << std::endl;
#endif
if (sz != sizeof(T))
throw runtime_error("CachedObj: wrong size object in operator new");
if (!freeList)
{
// ths list is empty: grab a new chunk of memory
// allocates chunk number of objects of type T
T *array = alloc_mem.allocate(chunk);
count += chunk;
// now set the next pointers in each object in the allocated memory
for (size_t i = 0; i != chunk; ++i)
{
cout<<"分配内存"<<i<<endl;
add_to_freelist(&array[i]);
}
}
T *p = freeList;
//为了防止派生类中也有next成员,所以在freeList->CachedObj<T>::next用类名进行限制访问next成员
freeList = freeList->CachedObj<T>::next;
cout<<"从freelist取得指针 p="<<p<<endl;
return p; // constructor of T will construct the T part of the object
}
template <typename T> void CachedObj<T>::operator delete (void *p, size_t)
{
#ifdef _DEBUG_
std::cout << "CachedObj<T>::operator delete" << std::endl;
#endif
if (p != 0)
{ // put the "deleted" object back at head of freelist
add_to_freelist(static_cast<T*>(p));
cout<<"增加到自由链表"<<p<<endl;
}
}
template <typename T> void CachedObj<T>::add_to_freelist (T *p)
{
p->CachedObj<T>::next = freeList;
freeList = p;
}
template <typename T> void CachedObj<T>::releaseMemory ()
{
//size_t i = 0;
//T* p;
////if(count > 0)
////{
//// alloc_mem.deallocate(freeList, count);
//// count = 0;
//// freeList = NULL;
////}
//while(i < count)
//{
// p = freeList;
// freeList = freeList->CachedObj<T>::next;
//
// //alloc_mem.deallocate(freeList, 1);
// free(p);
//}
}
template <typename T> CachedObj<T>::~CachedObj()
{
}
//声明静态的变量和方法
template <typename T> allocator<T> CachedObj<T>::alloc_mem;
template <typename T> T* CachedObj<T>::freeList = 0;
template <typename T> const size_t CachedObj<T>::chunk = 24;
template <typename T> size_t CachedObj<T>::count = 0;
template <typename T>
class Gtest: public CachedObj< Gtest<T> >
{
public:
T t;
Gtest():t(0) { }
};
int main ()
{
Gtest<int> *t = new Gtest<int>;
delete t;
Gtest<float> *f = new Gtest<float>;
delete f;
//可见要对每一个类型进行释放内存
cout<<Gtest<int>::count<<endl;
cout<<Gtest<int>::count<<endl;
//cout<<Gtest<float>::count<<endl;
return 0;
}