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.
首先,至少有4-5个主题有一个类似的主题。我读了每一本,我觉得它们并没有真正帮助我解决这个问题。如果有人发现重复的问题,我道歉。在发布这篇文章之前,我已经完成了我的搜索任务,因为这似乎是一个非常常见的问题。
I'm using Visual Studio .NET 2003 on Windows 7.
我正在Windows 7上使用Visual Studio . net 2003。
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.
我有自己的重载new/delete,指向我自己对malloc()和free()的自定义调用进行诊断。我的新/删除重载包含在一个头文件中,其中包含了一些文件。
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。
In my tests I've found that STL is still mixing calls to my own new/delete and the standard MSVC new/delete calls.
在我的测试中,我发现STL仍然混合调用我自己的新/delete和标准的MSVC 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?
将我的头文件包含在数千个其他文件中似乎不太现实,这将花费太多的时间。任何人都可以提供一些关于如何正确有效地重载新的/删除的提示,所以所有的东西都使用我的自定义内存管理器吗?
2 个解决方案
#1
66
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:
事情不是这样的。替换这两个操作符,这是在链接时完成的。您所需要做的就是编写一个单独的TU来定义这些操作符并将其链接到混合中。没有人需要知道这个:
// 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.
原则上,不需要任何头文件来声明这些函数(操作符new、操作符delete),因为这两个函数的声明已经硬编码到语言中了。但是,std、std: bad_alloc和std::size_t的名称并没有预先声明,所以您可能需要包含
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).
在c++ 11及以上版本中,您还可以使用decltype(sizeof(0))以一种不需要任何库的方式获取第一个参数的大小。c++ 11还有一个更简单的异常模型,没有动态异常规范(最终完全用c++ 17从语言中删除)。
void * operator new(decltype(sizeof(0)) n) noexcept(false)
{
//...
}
#2
31
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
}
#1
66
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:
事情不是这样的。替换这两个操作符,这是在链接时完成的。您所需要做的就是编写一个单独的TU来定义这些操作符并将其链接到混合中。没有人需要知道这个:
// 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.
原则上,不需要任何头文件来声明这些函数(操作符new、操作符delete),因为这两个函数的声明已经硬编码到语言中了。但是,std、std: bad_alloc和std::size_t的名称并没有预先声明,所以您可能需要包含
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).
在c++ 11及以上版本中,您还可以使用decltype(sizeof(0))以一种不需要任何库的方式获取第一个参数的大小。c++ 11还有一个更简单的异常模型,没有动态异常规范(最终完全用c++ 17从语言中删除)。
void * operator new(decltype(sizeof(0)) n) noexcept(false)
{
//...
}
#2
31
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
}