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_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)
{
//...
}
这篇关于如何正确更换全球新&删除操作符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何正确更换全球新&删除操作符
基础教程推荐
- C++,'if' 表达式中的变量声明 2021-01-01
- 如何定义双括号/双迭代器运算符,类似于向量的向量? 2022-01-01
- 运算符重载的基本规则和习语是什么? 2022-10-31
- C++ 标准:取消引用 NULL 指针以获取引用? 2021-01-01
- 如何在 C++ 中处理或避免堆栈溢出 2022-01-01
- C++ 程序在执行 std::string 分配时总是崩溃 2022-01-01
- 设计字符串本地化的最佳方法 2022-01-01
- 您如何将 CreateThread 用于属于类成员的函数? 2021-01-01
- 调用std::Package_TASK::Get_Future()时可能出现争用情况 2022-12-17
- 什么是T&&(双与号)在 C++11 中是什么意思? 2022-11-04