C ++重载运算符new delete,

编程入门 行业动态 更新时间:2024-10-24 14:22:24
本文介绍了C ++重载运算符new delete,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这有点复杂,所以请让我知道这是否没有道理,我们的团队正在编写C ++应用程序,并且我们以前已重载了运算符new.最近,我遇到了这篇文章: www.flipcode/archives/How_To_Find_Memory_Leaks.shtml 关于如何通过我们的内存分配获取调试信息.

Hi this is a little complicated so please let me know if any of this does not make sense, our team is writing a C++ application and we have previously had operator new overloaded. Recently I ran into this article: www.flipcode/archives/How_To_Find_Memory_Leaks.shtml about how to get debug information with our memory allocations.

应用程序内的所有文件#包括一个我们具有编译时平台配置的文件,在该文件中,我添加了以下内容:

All the files within the application #include one file where we have compile-time platform configurations, and within that file I added the following:

#ifdef _DEBUG void* operator new(size_t size, const char *filename, const char *funcname, int line); void* operator new[](size_t size, const char *filename, const char *funcname, int line); #define new new(__FILE__, __FUNCSIG__, __LINE__) #endif

由于我们仅链接libcmt.lib进行平台构建,为了使用STL,我删除了旧的操作符new的实现,该实现如下所示:

Since we only link libcmt.lib for our platform build, to use the STL I removed our old implementation of operator new which looked like:

// in a .cpp file: void* operator new(size_t size) { ... }

并替换为:

// in the same .cpp file as above... #undef new void* operator new(size_t size, const char *filename, const char *funcname, int line) { ... } #define new new(__FILE__, __FUNCSIG__, __LINE__)

这可以很好地进行编译,但是我从libcmt.lib中收到了很多链接器错误:

this works fine for compiling, but I'm getting a bunch of linker errors from libcmt.lib:

例如:libcmt.lib(malloc.obj):错误LNK2001:无法解析的外部符号__imp_HeapAlloc

ex: libcmt.lib(malloc.obj) : error LNK2001: unresolved external symbol __imp_HeapAlloc

重新添加运算符new的旧实现(不带附加参数)使链接器成功链接所有内容.

Adding back the old implementation of operator new (without the additional parameters) allows the linker to link everything successfully.

我的问题:我希望libcmt看到我的宏(#define new new( FILE , FUNCSIG , LINE )),因此何时它链接尝试并链接我定义的版本(带有调试宏).

My question: I want libcmt to see my macro (#define new new(FILE, FUNCSIG, LINE)) and thus when it links try and link the version I defined (with the debug macros).

如何使它正常工作? (我还尝试使用Visual Studio中的属性表来定义宏)

How do I get this to work?? (I also tried using the property sheets within visual studio to define the macro)

推荐答案

您无法使其正常运行.如果在包含标准头的任何文件中定义了此宏,则该行为是未定义的.当然,项目的正常演变将导致人们定义类local operator new,或使用new放置,或使用此宏将破坏的多种技术中的任何一种.它与#define while if处于同一级别.即使您不使用标准库,重新定义宏中的关键字也是遇到麻烦的肯定方法.

You can't get it to work. If this macro is defined in any file that includes a standard header, the behavior is undefined. And of course, normal evolution of a project will lead people to define class local operator new, or use placement new, or any one of a number of techniques which this macro will break. It's on about the same level as #define while if. Redefining a keyword in a macro is a sure way of getting into trouble, even if you don't use the standard library.

更多推荐

C ++重载运算符new delete,

本文发布于:2023-06-03 12:16:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/473744.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:运算符   delete

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!