为 new 运算符编写重载函数
我编写了一个简单的函数,如下所示:
void *operator new(size_t size) throw(std::bad_alloc)
{
void *p;
p = malloc(size);
if(!p)
throw bad_alloc();
return p;
}
我还能做些什么来改进这个? malloc
会比 new
更有效吗?如果我想写 new[]
我只需要更改函数签名吗?
I have written a simple function as following:
void *operator new(size_t size) throw(std::bad_alloc)
{
void *p;
p = malloc(size);
if(!p)
throw bad_alloc();
return p;
}
What else can i do to improve this? Would malloc
be more effective than new
? If I want to write new[]
do I only need to change just the function signature?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这段代码将按原样工作,但是如果你这样做,你几乎需要编写一个匹配的
::operator delete
来使用它:就我个人而言,我可能会将您的代码修改为更像这样的内容:
我更喜欢初始化我能做的一切,而不是创建一个未初始化的变量,然后才为其赋值。在这种情况下,差异很小,但我认为这是一个值得培养的习惯。
至于比
operator new
的默认实现更有效,我想说的是,它不会更有效,而且很可能效率较低。您提供的基本上是在 C++ 诞生之前实现了多少个标准库,但从那时起,许多(大多数?)在::operator new
的实现上做了更多工作来定制它更接近于 C++ 中动态分配内存的使用方式(其中 malloc 主要面向 C 中的使用方式,通常至少有一点不同)。就
new[]
而言,是的,这只是函数签名的更改。尽管用于单分配和多分配,但对operator new和operator new[]`的要求是相同的。This code will work the way it is, but if you do this, you pretty much need to write a matching
::operator delete
that will work with it:Personally, I'd probably modify your code to something more like:
I prefer to initialize everything I can, rather than create an uninitialized variable, and only later assign a value to it. In this case, the difference is pretty minor, but I consider it a habit worth cultivating.
As far as being more effective than the default implementation of
operator new
, I'd say chances are that no, it won't be more effective, and might well be less effective. What you've provided is basically how many standard libraries were implemented toward the dawn of C++, but since then, many (most?) have done more work on their implementation of::operator new
to tailor it more closely to how dynamically allocated memory tends to be used in C++ (wheremalloc
is mostly oriented toward how it's used in C, which is typically at least a little different).As far as
new[]
goes, yes, it's just a change of function signature. Despite being used for single vs. multiple allocations, the requirements onoperator new
and operator new[]` are identical.如果您替换全局
operator new
,您还应该替换nothrow
变体。当然,还有全局运算符delete
(普通和nothrow变体,因为如果对象的构造函数抛出异常,operator delete
的nothrow
变体code> 被调用为nothrot
operator new
)。我想在大多数实现中,内置的operator new 看起来或多或少与您的替代品完全相同。无论如何,一般来说,我不希望您能够击败
operator new
的内部实现。If you replace global
operator new
you also should replace thenothrow
variant. And, of course, global operatordelete
(both the normal and nothrow variant, because if the constructor of the object throws, thenothrow
variant ofoperator delete
is called fornothrow
operator new
).I guess in most implementations the built-in
operator new
looks more or less exactly like your replacement. Anyway, in general I'd not expect that you can beat the internal implementation ofoperator new
.operator new
负责分配对象的底层内存。某些应用程序使用malloc
以外的其他分配方案来分配内存。一个例子是池分配:应用程序从操作系统请求大量内存并管理该内存本身的方式。这可以减少系统调用的开销,防止碎片或为内存分配提供操作系统通常不会提供的时间保证。如果您不知道这是否可以提高程序的性能,那么这对您来说可能并不重要。
operator new
is responsible for allocating the underlying memory of an object. Some applications use other allocation schemes thanmalloc
to allocate memory. One example is pool allocation: The application requests a large amount of memory from the operating system and manages how that memory itself. This can safe the overhead of a system call, prevent fragmentation or provide time guarantees for memory allocation that the operating system usually wont.If you don't know if this could improve the performance of your program, it probably does not matter for you.