#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)
怎么参数个数不一样呢?
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)
怎么参数个数不一样呢?
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
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_NEWbefore 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.
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
函数的第1参数是sizeof(int),第2参数是THIS_FILE,第3参数是__LINE__。
比如
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);