当我尝试在VS2012中覆盖默认内存分配函数时,出现以下链接器错误:
1> Main.obj:error LNK2005:“void * __cdecl operator new(unsignedint)”(?? 2 @ YAPAXI@Z)已在MSVCRTD.lib(MSVCR110D.dll)中定义
1> Main.obj:.error LNK2005:“void __cdecl operator delete(void *)”(?? 3 @ YAXPAX@Z)已在MSVCRTD.lib(MSVCR110D.dll)中定义
1> c:\ users \ documents \ visual studio 2012 \ Projects \ CustomMemoryAllocator \ Debug \ CustomMemoryAllocator.exe:fatal error LNK1169:找到一个或多个重复定义的符号
以下是我的代码(没有智能感应错误):
1> Main.obj:error LNK2005:“void * __cdecl operator new(unsignedint)”(?? 2 @ YAPAXI@Z)已在MSVCRTD.lib(MSVCR110D.dll)中定义
1> Main.obj:.error LNK2005:“void __cdecl operator delete(void *)”(?? 3 @ YAXPAX@Z)已在MSVCRTD.lib(MSVCR110D.dll)中定义
1> c:\ users \ documents \ visual studio 2012 \ Projects \ CustomMemoryAllocator \ Debug \ CustomMemoryAllocator.exe:fatal error LNK1169:找到一个或多个重复定义的符号
以下是我的代码(没有智能感应错误):
#include <iostream>
using namespace std;
void *operator new(size_t size){
if(void *mem = malloc(size)){
cout << "allocated memory" << endl;
return mem;
}
else{
throw bad_alloc();
}
}
void operator delete(void* mem) throw() {
cout << "deleting" << endl;
free(mem);
}
int main(){
cout << "test";
int* a = new int(4);
delete a;
int b = 0;
cin >> b;
}
有人能帮忙吗?
这段内容与IT技术无关。
new
、delete
的重载会严重失败。微软页面详细解释了原因。那些pragma
指令是为了让链接器卸载这些库。据我所知,这只会在VC++上发生。 - P0W