問題描述
首先,至少有 4-5 個主題與 SO 上的主題相似.我閱讀了其中的每一個,但我覺得它們并沒有真正幫助我解決這個特定問題.如果其他人發現重復的問題,我深表歉意.在發布此內容之前,我已經完成了我的搜索,因為這似乎是一個非常常見的問題.
First of all, there were at least 4-5 topics with a similar topic on SO. I read each of them and I don't feel they really help me with this specific issue. If someone else finds a duplicate question I apologize. I've done my share of searching before I posted this, as it seems like a very common question.
我在 Windows 7 上使用 Visual Studio .NET 2003.
I'm using Visual Studio .NET 2003 on Windows 7.
我有自己的 new/delete 重載,指向我自己對 malloc() 和 free() 的自定義調用以進行診斷.我的新/刪除重載位于我已包含在幾個文件中的頭文件中.
I have my own overloads of new/delete that point to my own custom calls to malloc() and free() for diagnostics. My new/delete overloads are in a header file which I've included in a few files.
問題是,代碼庫幾乎像意大利面條一樣,沒有簡單的方法可以確保所有東西都使用這些重載.有包含到第三方庫的黑盒.我們也到處使用 STL.
The problem is, the code base is pretty much spaghetti and there is no easy way to make sure these overloads get used by everything. There are includes to third party libraries that are black-box. We also use STL everywhere.
在我的測試中,我發現 STL 仍在混合調用我自己的 new/delete 和標準的 MSVC new/delete 調用.
In my tests I've found that STL is still mixing calls to my own new/delete and the standard MSVC new/delete calls.
將我的頭文件包含在數以千計的其他文件中似乎不太現實,這會花費太長時間.任何人都可以提供一些關于如何正確有效地全局重載 new/delete 以便一切都使用我的自定義內存管理器的技巧嗎?
It doesn't seem realistic to include my header file in thousands of other files, that would just take far too long. Can anyone offer some tips on how to properly and effectively overload new/delete globally so everything uses my custom memory manager?
推薦答案
這不是這樣的.您替換這兩個運算符,這是在鏈接時完成的.您需要做的就是編寫一個定義這些運算符并將其鏈接到組合中的單個 TU.沒有其他人需要知道這一點:
That's not how this works. You replace the two operators, and this is done at link time. All you need to do is write a single TU that defines these operators and link it into the mix. Nobody else ever needs to know about this:
// optional_ops.cpp
void * operator new(std::size_t n) throw(std::bad_alloc)
{
//...
}
void operator delete(void * p) throw()
{
//...
}
原則上,不需要任何頭文件來聲明這些函數(operator new
, operator delete
),因為聲明如果您愿意,這兩個函數已經被硬編碼到語言中.但是,名稱 std
、std::bad_alloc
和 std::size_t
是未預先聲明的,因此您將可能想要包含
或其他一些標題來提供這些名稱.
In principle, there's no need for any header files to declare these functions (operator new
, operator delete
), since the declarations of those two functions are already hardcoded into the language, if you will. However, the names std
, std::bad_alloc
and std::size_t
are not predeclared, so you will probably want to include <new>
or some other header to provide those names.
在 C++11 及更高版本中,您也可以使用 decltype(sizeof(0))
以不需要任何類型庫的方式獲取第一個參數的大小.C++11 還有一個更簡單的異常模型,沒有動態異常規范(最終在 C++17 中完全從語言中刪除).
In C++11 and beyond, you can alternatively use decltype(sizeof(0))
to get the size of the first parameter in a way that doesn't require any kind of library. C++11 also has a simpler exception model without dynamic exception specifications (which were finally removed from the language entirely in C++17).
void * operator new(decltype(sizeof(0)) n) noexcept(false)
{
//...
}
這篇關于如何正確更換全球新&刪除操作符的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!