七猫的藏经阁

其实只是垃圾箱

VC知识库BLOG 首页 新随笔 联系 聚合 登录
  194 Posts :: 0 Stories :: 629 Comments :: 4 Trackbacks

公告

其实我们每个人都是井底之蛙,最多在不同的井而已。

留言簿(3)

随笔分类

随笔档案

文章分类

文章档案

相册

收藏夹

好友

搜索

最新评论

阅读排行榜

评论排行榜

得承认,这不困难,下面的这种做法可能看起来舒服些。

#include

namespace HL
{
class mallocHeap
{
public:

~mallocHeap (void) {}

inline void * malloc (size_t sz)
{
return ::malloc (sz);
}


inline void free (void * ptr)
{
::free (ptr);
}

inline size_t getSize (void * ptr)
{
return ::_msize (ptr);
}
};

template
class PerClassHeap
{
public:
inline void * operator new (size_t sz)
{
printf("Malloc one\n");
return getHeap()->malloc (sz);
}
inline void operator delete (void * ptr)
{
printf("delete one\n");
getHeap()->free (ptr);
}
inline void * operator new[] (size_t sz)
{
return getHeap()->malloc (sz);
}
inline void operator delete[] (void * ptr)
{
getHeap()->free (ptr);
}
// For some reason, g++ needs placement new to be overridden
// as well, at least in conjunction with use of the STL.
// Otherwise, this should be superfluous.
inline void * operator new (size_t sz, void * p) { return p; }
inline void * operator new[] (size_t sz, void * p) { return p; }

private:
inline static SuperHeap * getHeap (void)
{
static SuperHeap theHeap;
return &theHeap;
}
};

}

class TestB : public HL::PerClassHeap
{

};
posted on 2008-08-06 13:31 Diviner 阅读(900) 评论(2)  编辑 收藏

Feedback

# re: 如何重载operator new,delete操作 2008-08-06 13:32 Diviner
hoard,还有其他很多内在分配器。

# re: 如何重载operator new,delete操作 2008-08-06 23:18 周星星
ref: http://blog.vckbase.com/bruceteen/articles/25581.html#32095

标题  
姓名  
主页
验证码 *
内容   
  登录  使用高级评论  Top
[使用Ctrl+Enter键可以直接提交]