new和DEBUG_NEW

armor2 2008-06-27 02:35:28
#define DEBUG_NEW new(THIS_FILE, __LINE__)
void* operator new(size_t nSize, LPCSTR lpszFileName, int nLine)
{
return NULL;
}
#define new DEBUG_NEW

请问
new(THIS_FILE, __LINE__)
void* operator new(size_t nSize, LPCSTR lpszFileName, int nLine)
怎么参数个数不一样呢?
...全文
109 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
anzhuge 2008-06-28
  • 打赏
  • 举报
回复
xx
pigsanddogs 2008-06-27
  • 打赏
  • 举报
回复
operator new 必须要求第一次个参数是size_t, 后面的是没有硬性规定, 可以随便定义。
比如
operator new(size_t, int, int, int, int)
则通过 new(int, int, int, int) class 的代码调用这个函数

只是c++规定了2个默认的实现,
一个是
operator new(size_t) // 普通调用 new class
一个是
operator new(size_t, void*) // replacement new, new (address) class
直接把address这个地址返回

vc的DEBUG_NEW之需要记录这个LPCSTR lpszFileName, int nLine两个信息,
所以实现了你看到的这个函数
经过了上述的宏后
new class 就变成了
new(THIS_FILE, __LINE__) class
于是编译器计算了下class的size后,开始调用下列函数,并期待返回一个可用的内存快。
void* operator new(size_t nSize, LPCSTR lpszFileName, int nLine);
cnzdgs 2008-06-27
  • 打赏
  • 举报
回复
int* p = new(THIS_FILE, __LINE__) int;
函数的第1参数是sizeof(int),第2参数是THIS_FILE,第3参数是__LINE__。
superdiablo 2008-06-27
  • 打赏
  • 举报
回复
new CMyClass实际上自动传入了一个sizeof(CMyClass)参数,明白了吧。
skinfeature 2008-06-27
  • 打赏
  • 举报
回复
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
cppwin 2008-06-27
  • 打赏
  • 举报
回复
CObject 对 operator new 有 3个重载形式

CObject::operator new

void* PASCAL operator new(
size_t nSize
);
void* PASCAL operator new(
size_t,
void* p
);
void* PASCAL operator new(
size_t nSize,
LPCSTR lpszFileName,
int nLine
);



In the Debug version, operator new participates in an allocation-monitoring scheme designed to detect memory leaks.

If you use the code line
#define new DEBUG_NEW

before any of your implementations in a .CPP file, then the second version of new will be used, storing the filename and line number in the allocated block for later reporting. You do not have to worry about supplying the extra parameters; a macro takes care of that for you.

Even if you do not use DEBUG_NEW in Debug mode, you still get leak detection, but without the source-file line-number reporting described above.


element_cn 2008-06-27
  • 打赏
  • 举报
回复
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

16,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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