gpt4 book ai didi

c++ - DEBUG_NEW 和 __FILE__ 有什么用?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:25:45 25 4
gpt4 key购买 nike

我看到了下面的宏,

#ifdef _DEBUG
#define new DEBUG_NEW
#UNDEF THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

上述宏的用法是什么?

谢谢

最佳答案

DEBUG_NEW 只是一个 MACRO,通常定义为:

#define DEBUG_NEW new(__FILE__, __LINE__)
#define new DEBUG_NEW

因此,无论您在哪里使用 new,它都可以跟踪文件和行号,这些文件和行号可用于定位程序中的内存泄漏。

__FILE____LINE__都是predefined macros在您使用它们的地方分别评估文件名和行号!

阅读下面的文章,它非常漂亮地解释了将 DEBUG_NEW 与其他有趣的宏一起使用的技术:

A Cross-Platform Memory Leak Detector


来自 Wikpedia ,

Debug_new refers to a technique in C++ to overload and/or redefine operator new and operator delete in order to intercept the memory allocation and deallocation calls, and thus debug a program for memory usage. It often involves defining a macro named DEBUG_NEW, and makes new become something like new(_FILE_, _LINE_) to record the file/line information on allocation. Microsoft Visual C++ uses this technique in its Microsoft Foundation Classes. There are some ways to extend this method to avoid using macro redefinition while still able to display the file/line information on some platforms. There are many inherent limitations to this method. It applies only to C++, and cannot catch memory leaks by C functions like malloc. However, it can be very simple to use and also very fast, when compared to some more complete memory debugger solutions.

关于c++ - DEBUG_NEW 和 __FILE__ 有什么用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5421754/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com