重载全局new运算符

//debug_new.h///Begin

#ifndef _DEBUG_NEW_H_
#define _DEBUG_NEW_H_
#ifdef _DEBUG
#undef new
extern void _RegDebugNew( void );
extern void* __cdecl operator new( size_t, const char*, int );
extern void __cdecl operator delete( void*, const char*, int);
#define new new(__FILE__, __LINE__)
#define REG_DEBUG_NEW _RegDebugNew();
#else
#define REG_DEBUG_NEW
#endif // _DEBUG
#endif // _DEBUG_NEW_H_

debug_new.hEnd

//debug_new.cpp///Begin

#ifdef _DEBUG
#include "stdafx.h"
#include <windows.h>
#include <crtdbg.h>
class _CriSec
{
CRITICAL_SECTION criSection;
public:
_CriSec()    { InitializeCriticalSection( &criSection ); }
~_CriSec()   { DeleteCriticalSection( &criSection );     }
void Enter() { EnterCriticalSection( &criSection );      }
void Leave() { LeaveCriticalSection( &criSection );      }
} _cs;
void _RegDebugNew( void )
{
_CrtSetDbgFlag( _CRTDBG_REPORT_FLAG | _CRTDBG_LEAK_CHECK_DF );
}
 void * __cdecl operator new( size_t nSize, const char* lpszFileName, int nLine )
 {
 
_cs.Enter();
 
void* p = _malloc_dbg( nSize, _NORMAL_BLOCK, lpszFileName, nLine );
 
_cs.Leave();
 
return p;
 }
void __cdecl operator delete( void* p, const char* /*lpszFileName*/, int /*nLine*/ )
{
_cs.Enter();
_free_dbg( p, _CLIENT_BLOCK );
_cs.Leave();
}

//debug_new.cpp///End

   上述代码在VS2008下报错,报错如下:

1>------ Build started: Project: Huang, Configuration: Debug Win32 ------

1>Compiling...

1>debug_new.cpp

1>c:\temp\huang\huang\debug_new.cpp(20) : warning C4229: anachronism used : modifiers on data are ignored

1>c:\temp\huang\huang\debug_new.cpp(20) : error C2365: 'operator new' : redefinition; previous definition was 'function'

1>c:\temp\huang\huang\debug_new.cpp(20) : error C2078: too many initializers

1>c:\temp\huang\huang\debug_new.cpp(20) : error C2440: 'initializing' : cannot convert from 'int' to 'void *'

1>        Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast

1>c:\temp\huang\huang\debug_new.cpp(20) : error C2143: syntax error : missing ';' before '('

1>c:\temp\huang\huang\debug_new.cpp(20) : error C2226: syntax error : unexpected type 'size_t'

1>c:\temp\huang\huang\debug_new.cpp(20) : error C2059: syntax error : ')'

1>c:\temp\huang\huang\debug_new.cpp(21) : error C2143: syntax error : missing ';' before '{'

1>c:\temp\huang\huang\debug_new.cpp(21) : error C2447: '{' : missing function header (old-style formal list?)

1>HuangView.cpp

1>c:\temp\huang\huang\huangview.cpp(12) : warning C4005: 'new' : macro redefinition

1>        c:\temp\huang\huang\debug_new.h(10) : see previous definition of 'new'

1>Generating Code...

1>Build log was saved at "file://c:\Temp\Huang\Huang\Debug\BuildLog.htm"

1>Huang - 8 error(s), 2 warning(s)

========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========




希望有谁可以帮我解决这个问题,或者帮我写个重载全局的你new 函数也行。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值