C++ 重载新建/删除
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/583003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
overloading new/delete
提问by Brammie
I'm making a little memory leak finder in my program, but my way of overloading new and delete (and also new[] and delete[]) doesn't seem to do anything.
我在我的程序中做了一个小内存泄漏查找器,但是我重载 new 和 delete(以及 new[] 和 delete[])的方式似乎没有做任何事情。
void* operator new (unsigned int size, const char* filename, int line)
{
void* ptr = new void[size];
memleakfinder.AddTrack(ptr,size,filename,line);
return ptr;
}
The way I overloaded new
is shown in the code snippet above. I guess it's something with the operator returning void* but I do not know what to do about it.
我重载的方式new
显示在上面的代码片段中。我想这是操作员返回 void* 的问题,但我不知道该怎么办。
采纳答案by dirkgently
void* ptr = new void[size];
Can't do that. Fix it.
不能那样做。修理它。
Never ever try to overload new/delete globally. Either have them in a base class and derive all your objects from this class or use a namespace or a template allocator parameter. Why, you may ask. Because in case your program is more than a single file and using STL or other libraries you are going to screw up.
永远不要尝试全局重载 new/delete。要么将它们放在基类中并从此类派生所有对象,要么使用命名空间或模板分配器参数。为什么,你可能会问。因为如果您的程序不仅仅是一个文件并且使用 STL 或其他库,您就会搞砸。
Here's a distilled version of new
operator from VS2005 new.cpp
:
这是new
来自 VS2005的运算符的蒸馏版本new.cpp
:
void * operator new(size_t size) _THROW1(_STD bad_alloc)
{ // try to allocate size bytes
void *p;
while ((p = malloc(size)) == 0)
if (_callnewh(size) == 0)
{ // report no memory
static const std::bad_alloc nomem;
_RAISE(nomem);
}
return (p);
}
回答by Dan
Never ever try to overload new/delete globally
永远不要尝试全局重载 new/delete
Why is it whenever someone tries to use a less common feature of C++, someone acts like it should never be done?
为什么每当有人试图使用 C++ 的一个不太常见的特性时,有人就会表现得好像永远不应该这样做?
It's done all the time, it is quite common, and I have not worked for a company that did not do this.
它一直在做,很常见,我还没有为一家没有这样做过的公司工作过。
Globally overloaded new
and delete
are extremely helpful in tracking memory, memory bugs, buffer overruns, etc.
全局过载new
,delete
在跟踪内存、内存错误、缓冲区溢出等方面非常有帮助。
Nobody in their right mind is going to go through a program with several million lines of code, and add a new and delete member to each and every class. That's just stupid.
头脑正常的人不会通过一个包含数百万行代码的程序,并为每个类添加一个新成员和删除成员。那只是愚蠢。
回答by kyku
Maybe you can do what you want with a little bit of preprocessor magic:
也许你可以用一点预处理器魔法来做你想做的事:
#include <iostream>
using namespace std;
void* operator new (size_t size, const char* filename, int line) {
void* ptr = new char[size];
cout << "size = " << size << " filename = " << filename << " line = " << line << endl;
return ptr;
}
#define new new(__FILE__, __LINE__)
int main() {
int* x = new int;
}
回答by T.E.D.
I think the problem here is that your new's parameter profile doesn't match that of the standard operator new, so that one isn't getting hidden (and is thus still being used).
我认为这里的问题是您的 new 的参数配置文件与标准运算符 new 的参数配置文件不匹配,因此不会被隐藏(因此仍在使用)。
Your parameter profiles for new and delete need to look like this:
new 和 delete 的参数配置文件需要如下所示:
void* operator new(size_t);
void operator delete(void*, size_t);
回答by Beno?t
The problem relies with the two arguments that you have added to the overloaded new operator. Try making filename and line global in some way (or member variables if you're overloading new and delete for a single class). That should work better.
问题依赖于您添加到重载 new 运算符的两个参数。尝试以某种方式使文件名和行成为全局变量(如果为单个类重载 new 和 delete,则为成员变量)。那应该效果更好。
回答by zvrba
Are you invoking the overloaded operator correctly, i.e., passing it the additional parameters?
您是否正确调用了重载运算符,即向其传递了附加参数?