日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

VC里的#define new DEBUG_NEW

發布時間:2024/6/14 c/c++ 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 VC里的#define new DEBUG_NEW 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

以下代碼常常在一個類文件的開頭出現,是什么意思呢?

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

先看MSDN里的解釋:

Remarks

Assists in finding memory leaks. You can use DEBUG_NEW everywhere in your program that you would ordinarily use the new operator to allocate heap storage.

In debug mode (when the _DEBUG symbol is defined), DEBUG_NEW keeps track of the filename and line number for each object that it allocates. Then, when you use the CMemoryState::DumpAllObjectsSince member function, each object allocated with DEBUG_NEW is shown with the filename and line number where it was allocated.

To use DEBUG_NEW, insert the following directive into your source files:

#define new DEBUG_NEW

Once you insert this directive, the preprocessor will insert DEBUG_NEW wherever you use new, and MFC does the rest. When you compile a release version of your program, DEBUG_NEW resolves to a simple new operation, and the filename and line number information is not generated.

?

? ? 這樣就很清楚了,當在debug模式下時,我們分配內存時的new被替換成DEBUG_NEW,而這個DEBUG_NEW不僅要傳入內存塊的大小,還要傳入源文件名和行號,這就有個好處,即當發生內存泄漏時,我們可以在調試模式下定位到該問題代碼處。若刪掉該句,就不能進行定位了。而在release版本下的new就是簡單的new,并不會傳入文件名和行號。
? ? 如果定義了_DEBUG,表示在調試狀態下編譯,因此相應修改了兩個符號的定義THIS_FILE是一個char數組全局變量,字符串值為當前文件的全路徑,這樣在Debug版本中當程序出錯時出錯處理代碼可用這個變量告訴你是哪個文件中的代碼有問題。

轉載于:https://www.cnblogs.com/imapla/archive/2012/09/03/2668859.html

總結

以上是生活随笔為你收集整理的VC里的#define new DEBUG_NEW的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。