問(wèn)題描述
首先,至少有 4-5 個(gè)主題與 SO 上的主題相似.我閱讀了其中的每一個(gè),但我覺(jué)得它們并沒(méi)有真正幫助我解決這個(gè)特定問(wèn)題.如果其他人發(fā)現(xiàn)重復(fù)的問(wèn)題,我深表歉意.在發(fā)布此內(nèi)容之前,我已經(jīng)完成了我的搜索,因?yàn)檫@似乎是一個(gè)非常常見(jiàn)的問(wèn)題.
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 重載,指向我自己對(duì) malloc() 和 free() 的自定義調(diào)用以進(jìn)行診斷.我的新/刪除重載位于我已包含在幾個(gè)文件中的頭文件中.
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.
問(wèn)題是,代碼庫(kù)幾乎像意大利面條一樣,沒(méi)有簡(jiǎn)單的方法可以確保所有東西都使用這些重載.有包含到第三方庫(kù)的黑盒.我們也到處使用 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.
在我的測(cè)試中,我發(fā)現(xiàn) STL 仍在混合調(diào)用我自己的 new/delete 和標(biāo)準(zhǔn)的 MSVC new/delete 調(diào)用.
In my tests I've found that STL is still mixing calls to my own new/delete and the standard MSVC new/delete calls.
將我的頭文件包含在數(shù)以千計(jì)的其他文件中似乎不太現(xiàn)實(shí),這會(huì)花費(fèi)太長(zhǎng)時(shí)間.任何人都可以提供一些關(guān)于如何正確有效地全局重載 new/delete 以便一切都使用我的自定義內(nèi)存管理器的技巧嗎?
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?
推薦答案
這不是這樣的.您替換這兩個(gè)運(yùn)算符,這是在鏈接時(shí)完成的.您需要做的就是編寫(xiě)一個(gè)定義這些運(yùn)算符并將其鏈接到組合中的單個(gè) TU.沒(méi)有其他人需要知道這一點(diǎn):
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()
{
//...
}
原則上,不需要任何頭文件來(lái)聲明這些函數(shù)(operator new
, operator delete
),因?yàn)?em>聲明如果您愿意,這兩個(gè)函數(shù)已經(jīng)被硬編碼到語(yǔ)言中.但是,名稱(chēng) std
、std::bad_alloc
和 std::size_t
是未預(yù)先聲明的,因此您將可能想要包含
或其他一些標(biāo)題來(lái)提供這些名稱(chēng).
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))
以不需要任何類(lèi)型庫(kù)的方式獲取第一個(gè)參數(shù)的大小.C++11 還有一個(gè)更簡(jiǎn)單的異常模型,沒(méi)有動(dòng)態(tài)異常規(guī)范(最終在 C++17 中完全從語(yǔ)言中刪除).
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)
{
//...
}
這篇關(guān)于如何正確更換全球新&刪除操作符的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!