分类: C/C++
2008-08-04 09:30:34
在文件头经常可以发现以下语句:
#ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif其中,将new定义为DEBUG_NEW
宏替换:#define DEBUG_NEW new(THIS_FILE, __LINE__) //文件名、行号被传入,供调试输出。
实际代码如下:
void* AFX_CDECL operator new(size_t nSize, LPCSTR lpszFileName, int nLine) { return ::operator new(nSize, _NORMAL_BLOCK, lpszFileName, nLine); } //分配内存 void* __cdecl operator new(size_t nSize, int nType, LPCSTR lpszFileName, int nLine) { #ifdef _AFX_NO_DEBUG_CRT UNUSED_ALWAYS(nType); UNUSED_ALWAYS(lpszFileName); UNUSED_ALWAYS(nLine); return ::operator new(nSize); #else void* pResult; #ifdef _AFXDLL _PNH pfnNewHandler = _pfnUninitialized; #endif for (;;) { pResult = _malloc_dbg(nSize, nType, lpszFileName, nLine); if (pResult != NULL) return pResult; #ifdef _AFXDLL if (pfnNewHandler == _pfnUninitialized) { AFX_MODULE_THREAD_STATE* pState = AfxGetModuleThreadState(); pfnNewHandler = pState-> m_pfnNewHandler; } if (pfnNewHandler == NULL || (*pfnNewHandler)(nSize) == 0) break; #else if (_afxNewHandler == NULL || (*_afxNewHandler)(nSize) == 0) break; #endif } return pResult; #endif } #endif //_DEBUG(全文完) 下载本文示例代码