久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何正確更換全球新&刪除操作符

How to properly replace global new amp; delete operators(如何正確更換全球新amp;刪除操作符)
本文介紹了如何正確更換全球新&刪除操作符的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(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) stdstd::bad_allocstd::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)于如何正確更換全球新&amp;刪除操作符的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

What is the fastest way to transpose a matrix in C++?(在 C++ 中轉(zhuǎn)置矩陣的最快方法是什么?)
Sorting zipped (locked) containers in C++ using boost or the STL(使用 boost 或 STL 在 C++ 中對(duì)壓縮(鎖定)容器進(jìn)行排序)
Rotating a point about another point (2D)(圍繞另一個(gè)點(diǎn)旋轉(zhuǎn)一個(gè)點(diǎn) (2D))
Image Processing: Algorithm Improvement for #39;Coca-Cola Can#39; Recognition(圖像處理:Coca-Cola Can 識(shí)別的算法改進(jìn))
How do I construct an ISO 8601 datetime in C++?(如何在 C++ 中構(gòu)建 ISO 8601 日期時(shí)間?)
Sort list using STL sort function(使用 STL 排序功能對(duì)列表進(jìn)行排序)
主站蜘蛛池模板: 国产精品成人久久久久 | 精品视频亚洲 | 国产精品欧美一区二区三区不卡 | 91麻豆精品国产91久久久久久久久 | 精品乱码一区二区三四区 | 欧美美乳 | 国产精品国产三级国产aⅴ无密码 | 在线日韩精品视频 | 超碰97免费| 天天夜夜操 | 搞av.com | 精品国产不卡一区二区三区 | 久久国产精品免费视频 | 久久精品国产亚洲a | 免费在线h视频 | 成人午夜毛片 | 国产精品永久在线观看 | 精品久久久久久久 | 一区二区三区免费 | 欧美日一区 | 国产精品美女久久久久aⅴ国产馆 | 欧美亚洲综合久久 | 亚洲电影第1页 | 国产一区二区免费在线 | 日本在线视频中文字幕 | 免费成人av | 二区在线视频 | 夜夜操天天艹 | 羞羞午夜| 网色| 日韩在线成人 | 精品视频成人 | 中文字幕 亚洲一区 | 黄色免费网站在线看 | 高清久久久 | 国产高清一区二区三区 | 男人的天堂视频网站 | 亚洲男人天堂 | 日日操日日干 | 成人精品一区二区户外勾搭野战 | 毛片在线视频 |