请教STL中自定义allocator需要注意的问题?

poiu_00cn 2003-10-21 07:30:02
由于系统需要长时间运行(一年),所以本人想写一个自定义的allocator,用来存储产生的动态对象,参考侯捷老师的存储池的概念写了一个存储代理类(mempool),测试良好。但是在实现自定义allocator的时候(继承allocator类,mempool成员变量),结果程序编译失败。

请问各位有人做过类似的工作么?请指教一二.
谢谢!
...全文
217 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
poiu_00cn 2003-10-24
  • 打赏
  • 举报
回复
yjh1982(血精灵) :
恩,忘写拷贝构造函数了,失败。VC支持不成员摸板么?我写了一段程序,实现成员摸板好像没有什么问题,只成员摸板构造函数带有初始化成员列表的情况下编译器不支持。

hz_tiger(乳虎) :
呵呵,小弟刚毕业,只有参考Bjarne Stroustrup他老人家的经典东东,见笑了。最近在看C++ STL,正好拿这个allocator练手。VC下的STL设计好像比较臃肿,而且和iterator的有些差别。

yjh1982 2003-10-23
  • 打赏
  • 举报
回复
allocator代码不是很多,用不着继承.另VC不支持成员模版
hz_tiger 2003-10-23
  • 打赏
  • 举报
回复
呵呵,是不是看一看C++之父写的《C++ PROGRAMMING LANGUAGE》,和你写的差不多,你的allocate 好像简单了一点:),在看一下标准模板库的实现。

C++之父:
Before applying this kind of optimization, make sure that it is necessary. I expect that many default allocators will implement exactly this kind of classic C++ optimization – thus saving you the bother.
poiu_00cn 2003-10-22
  • 打赏
  • 举报
回复
yjh1982(血精灵) :
请问vc的接口不是遵循C++ STL标准的么?
如果我继承allocator<T> 来实现公共接口,而在内存分配部分采用自己的存储方式,这样可行性有多大?
poiu_00cn 2003-10-22
  • 打赏
  • 举报
回复
但是用myPool实现私有分配器的时候出现一个问题:
template <class T>
class myAlloc
{
private:
static myPool<T> pool;

public:
typedef T value_type;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;

pointer address(reference r) const { return &r; }
const_pointer address(const_reference r) const { return &r; }

myAlloc() throw() ;
template <class U> myAlloc(const myAlloc<U>&) throw() ;
~myAlloc() throw() ;

pointer allocate(size_type n, myAlloc<void>::const_pointer hint = 0) ; void deallocate(pointer p, size_type n) ;
void construct(pointer p, const T& val) { new(p) T(val) ; }
void destroy(pointer p){ p->~T() ; }
size_type max_size() const throw() ;
template <class U>
struct rebind { typedef myAlloc<U> other; };
};
template<class T> bool operator==(const myAlloc<T>&, const myAlloc<T>&) throw() ;
template<class T> bool operator!=(const myAlloc<T>&, const myAlloc<T>&) throw() ;

template<class T>
myPool<T> myAlloc<T>::pool(sizeof(T));
//error C2954: template definitions cannot nest
//'pool' : 'class myPool<T> (void)' differs in levels of indirection from 'class myPool<T>'

请问是不是成员初始化在嵌套模板类中不能实现?
有没有其他的替代方法实现?

poiu_00cn 2003-10-22
  • 打赏
  • 举报
回复
呵呵,我好像想的简单了点,以为能轻松的实现呢。
下面是我的代码,一部分是参考《effective C++》中内存篇写的

#include <list>

template <class T>
class myPool
{
public:
myPool(unsigned int sz);
~myPool();

void Clear();
void free(void* p );
void* alloc(size_t size);

std::list<T*> pList; //maintain allocated T pointer;
std::list<char*> allocList; //maintain allocated heap pointor;
const int esize; // T object size;
};


template <class T>
myPool<T>::myPool(unsigned int sz):esize(sz)
{

}

template<class T>
myPool<T>::~myPool()
{
//free heap;
std::list<char*>::iterator i = allocList.begin();
for( ;i != allocList.end();i++)
{
char* p = *i;
delete p;
}
}

template<class T>
void* myPool<T>::alloc(size_t size)
{
std::list<T*>::iterator i = pList.begin();
if(pList.size() == 0)
{
int newlength = 5*size; //One time alloc 5 object;

char* mem = new char[newlength];
allocList.insert(allocList.begin(),mem);

T* head = reinterpret_cast<T*>(mem); // alloc a sequence heap space ;
for(int j=0; j<10; j++)
{
pList.insert(pList.begin(),&head[j]);
}
}
i = pList.begin();
T* ret = *i;
pList.remove(*i);

return ret;
}
template<class T>
void myPool<T>::free(void *p)
{
pList.insert(pList.begin(), static_cast<T*>(p));
}


// class air for test myPool
class air
{
public:
air();
virtual ~air();

void* operator new(size_t);
void operator delete(void* p);

double a;
static myPool<air> pool;
}; //sizeof(air) = 10 byte;

myPool<air> air::pool(sizeof(air));

air::air():a(0){}

air::~air(){}

void* air::operator new(size_t size)
{
return pool.alloc(size);
}

void air::operator delete(void *p)
{
pool.free(p);
}

void main()
{
air* a1 = new air;
air* a2 = new air;
air* a3 = new air;
air* a4 = new air;
air* a5= new air;

a1->a = 11;
a2->a = 12;
a3->a = 13;
a4->a = 14;
a5->a = 15;

delete a1;
delete a2;
delete a3;
delete a4;
delete a5;

air* b1 = new air;
delete b1;
}

这段程序能正常运行,达到了预期的目的:在堆上分配固定内存来存放实体类;
yjh1982 2003-10-22
  • 打赏
  • 举报
回复
关于这个要注意:有些分配器实现不是标准的,因此要copy你现在使用的编译器的分配器实现,
然后一模一样的实现对等的接口
北极猩猩 2003-10-21
  • 打赏
  • 举报
回复
贴一下代码,和错误信息吧。
这是没办法说的
Wolf0403 2003-10-21
  • 打赏
  • 举报
回复
错误是什么?是不是因为有的 allocator 接口没实现?
ttlb 2003-10-21
  • 打赏
  • 举报
回复
up
Andy84920 2003-10-21
  • 打赏
  • 举报
回复
好像这并不是一个简单问题吧?

内存分配器耶!

24,855

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧