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

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

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

問題描述

首先,至少有 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_allocstd::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)
{
  //...
}

這篇關于如何正確更換全球新&amp;刪除操作符的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

What is the fastest way to transpose a matrix in C++?(在 C++ 中轉置矩陣的最快方法是什么?)
Sorting zipped (locked) containers in C++ using boost or the STL(使用 boost 或 STL 在 C++ 中對壓縮(鎖定)容器進行排序)
Rotating a point about another point (2D)(圍繞另一個點旋轉一個點 (2D))
Image Processing: Algorithm Improvement for #39;Coca-Cola Can#39; Recognition(圖像處理:Coca-Cola Can 識別的算法改進)
How do I construct an ISO 8601 datetime in C++?(如何在 C++ 中構建 ISO 8601 日期時間?)
Sort list using STL sort function(使用 STL 排序功能對列表進行排序)
主站蜘蛛池模板: 91精品91久久久中77777 | 97精品国产 | 日韩精品视频网站 | 一区二区三区高清 | 人人爽av | 国产伦理一区 | 久久成人精品 | 吃奶动态图 | 天天做天天操 | 九九国产精品视频 | 亚洲五月婷婷 | 国产精品一区在线播放 | 黄色片网站免费 | 在线观看日韩欧美 | 久久久精品国产sm调教网站 | 色婷婷视频在线观看 | 久久久久国产一区二区三区 | 欧美一级黄色大片 | 久久久久久久久国产 | 国产亚洲一区二区三区 | 亚洲精品911| 欧美一区二区在线 | 精品国产一二三区 | 亚洲免费黄色 | 九色自拍| 日本三级视频在线观看 | 福利一区福利二区 | 手机av片| 黄色大毛片 | 99re在线 | 久久精品在线 | 日韩福利在线观看 | 久久天堂网 | 欧美成人一区二区三区 | av片在线免费观看 | 日韩小视频在线观看 | 国产精品国产精品国产专区不片 | 欧美一区二区精品 | 亚洲小视频在线观看 | 日韩中文av | 久久精品一区二区三区四区 |