为 new 运算符编写重载函数

发布于 2024-12-16 12:27:01 字数 300 浏览 1 评论 0原文

我编写了一个简单的函数,如下所示:

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 技术交流群。

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

发布评论

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

评论(3

绮筵 2024-12-23 12:27:01

这段代码将按原样工作,但是如果你这样做,你几乎需要编写一个匹配的::operator delete来使用它:

void operator delete(void *block) throw() {
    ::free(block);
}

就我个人而言,我可能会将您的代码修改为更像这样的内容:

void *operator new(size_t size) throw(std::bad_alloc)
{
    void *p =  malloc(size);
    if(!p)
       throw bad_alloc();
    return p;
}

我更喜欢初始化我能做的一切,而不是创建一个未初始化的变量,然后才为其赋值。在这种情况下,差异很小,但我认为这是一个值得培养的习惯。

至于比 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:

void operator delete(void *block) throw() {
    ::free(block);
}

Personally, I'd probably modify your code to something more like:

void *operator new(size_t size) throw(std::bad_alloc)
{
    void *p =  malloc(size);
    if(!p)
       throw bad_alloc();
    return p;
}

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++ (where malloc 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 on operator new and operator new[]` are identical.

岁月打碎记忆 2024-12-23 12:27:01

如果您替换全局operator new,您还应该替换nothrow 变体。当然,还有全局运算符delete(普通和nothrow变体,因为如果对象的构造函数抛出异常,operator deletenothrow变体code> 被调用为 nothrot operator new)。

我想在大多数实现中,内置的operator new 看起来或多或少与您的替代品完全相同。无论如何,一般来说,我不希望您能够击败 operator new 的内部实现。

If you replace global operator new you also should replace the nothrow variant. And, of course, global operator delete (both the normal and nothrow variant, because if the constructor of the object throws, the nothrow variant of operator delete is called for nothrow 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 of operator new.

夏花。依旧 2024-12-23 12:27:01

operator new 负责分配对象的底层内存。某些应用程序使用 malloc 以外的其他分配方案来分配内存。一个例子是池分配:应用程序从操作系统请求大量内存并管理该内存本身的方式。这可以减少系统调用的开销,防止碎片或为内存分配提供操作系统通常不会提供的时间保证。

如果您不知道这是否可以提高程序的性能,那么这对您来说可能并不重要。

operator new is responsible for allocating the underlying memory of an object. Some applications use other allocation schemes than malloc 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文