重载全局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.h/End

/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
    评论
C++中的new运算符用于动态分配内存并返回指向该内存的指针。它的语法如下: ``` new data-type; ``` 其中,data-type是要分配内存的数据类型。例如,要分配一个整数的内存,可以使用以下代码: ```c++ int *p = new int; ``` 这将分配一个整数的内存,并将指向该内存的指针存储在p中。要释放这个内存,可以使用delete运算符: ```c++ delete p; ``` 这将释放p指向的内存。 除了上述用法外,new运算符还可以用于分配数组和自定义类型的内存。对于数组,可以使用以下语法: ```c++ new data-type [size]; ``` 其中,size是数组的大小。例如,要分配一个包含5个整数的数组,可以使用以下代码: ```c++ int *p = new int[5]; ``` 这将分配一个包含5个整数的数组,并将指向该数组的指针存储在p中。要释放这个数组,可以使用delete[]运算符: ```c++ delete[] p; ``` 对于自定义类型,可以重载newdelete运算符来控制内存的分配和释放。例如,以下代码演示了如何重载newdelete运算符来分配和释放MyClass类型的内存: ```c++ class MyClass { public: void* operator new(size_t size) { void* p = ::operator new(size); // do something return p; } void operator delete(void* p) { // do something ::operator delete(p); } private: // data members }; ``` 在这个例子中,重载new运算符使用全局的operator new函数来分配内存,并在分配内存后执行一些自定义操作。重载delete运算符也执行一些自定义操作,并使用全局的operator delete函数来释放内存。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值