如何正确更换全球新产品删除运算符

发布于 2024-12-17 03:40:04 字数 475 浏览 1 评论 0原文

(SO 上至少有 4-5 个具有类似主题的主题。我阅读了每个主题,但我认为它们并没有真正帮助我解决这个特定问题)。

我在 Windows 7 上使用 Visual Studio .NET 2003。

我有自己的 new/delete 重载,它们指向我自己对 malloc()free() 的自定义调用> 用于诊断。我的新/删除重载位于头文件中,我已将其包含在几个文件中。

问题是,代码库几乎是意大利面条,没有简单的方法来确保这些重载被所有东西使用。其中包含黑盒第三方库。我们也到处使用STL。

在我的测试中,我发现 STL 仍然混合调用我自己的 new/delete 和标准 MSVC new/delete 调用。

将我的头文件包含在数千个其他文件中似乎不太现实,这会花费太长时间。任何人都可以提供一些关于如何正确且有效地全局重载 new/delete 以便所有内容都使用我的自定义内存管理器的提示吗?

(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).

I'm using Visual Studio .NET 2003 on Windows 7.

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.

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.

In my tests I've found that STL is still mixing calls to my own new/delete and the standard MSVC new/delete calls.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冰魂雪魄 2024-12-24 03:40:04

事情不是这样的。您替换这两个运算符,这是在链接时完成的。您所需要做的就是编写一个 TU 来定义这些运算符并将其链接到混合中。其他人不需要知道这一点:

// optional_ops.cpp

void * operator new(std::size_t n) throw(std::bad_alloc)
{
  //...
}
void operator delete(void * p) throw()
{
  //...
}

原则上,不需要任何头文件来声明这些函数(operator newoperator delete),因为如果您愿意的话,这两个函数的声明已经被硬编码到语言中。但是,名称 stdstd::bad_allocstd::size_t预先声明的,因此您将可能想要包含 或其他一些标头来提供这些名称。

在 C++11 及更高版本中,您也可以使用 decltype(sizeof(0)) 以不需要任何类型的库的方式获取第一个参数的大小。 C++11 还有一个更简单的异常模型,没有动态异常规范(最终在 C++17 中完全从语言中删除)。

void * operator new(decltype(sizeof(0)) n) noexcept(false)
{
  //...
}

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()
{
  //...
}

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.

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)
{
  //...
}
铁轨上的流浪者 2024-12-24 03:40:04

还要添加这些行:

void *operator new[](std::size_t s) throw(std::bad_alloc)
{
    // TODO: implement
    return NULL;
}
void operator delete[](void *p) throw()
{
    // TODO: implement
}

Also add these lines:

void *operator new[](std::size_t s) throw(std::bad_alloc)
{
    // TODO: implement
    return NULL;
}
void operator delete[](void *p) throw()
{
    // TODO: implement
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文