如何保存指向重新定义的运算符的指针?

发布于 2024-12-22 06:29:55 字数 819 浏览 1 评论 0原文

我已经重载了 new 和 delete 运算符。我想保存指向“旧”newdelete 的指针,以将其调用为“new”newdelete 。例如:

#include "h.h"
void * operator new ( size_t size, /*args*/ ) throw (std::bad_alloc)
{
    void * p = 0;
    p = original_new(size); //calling for 'old' new
    //some code
    return p;
}

以及类似的操作符delete

因此,我尝试在头文件中键入以下内容:

static void * (*original_new)(size_t) = ::operator new;
static void * (*original_new_arr)(size_t) = ::operator new[];
static void (*original_delete)(void *) = ::operator delete;
static void (*original_delete_arr)(void *) = ::operator delete[];

它已成功编译,但我在启动时有核心转储。 在 new 中调用 malloc 是可能的,这确实是一个想法。可以调用 new(std::nothrow) 但它也很糟糕。

I have overloaded new and delete operators. I want to save pointers to 'old' new and delete to call it into 'new' new and delete. For example:

#include "h.h"
void * operator new ( size_t size, /*args*/ ) throw (std::bad_alloc)
{
    void * p = 0;
    p = original_new(size); //calling for 'old' new
    //some code
    return p;
}

And the similar operator delete.

So, I'm trying in my header file to type the following:

static void * (*original_new)(size_t) = ::operator new;
static void * (*original_new_arr)(size_t) = ::operator new[];
static void (*original_delete)(void *) = ::operator delete;
static void (*original_delete_arr)(void *) = ::operator delete[];

It is successfully compiled, but I have core dump on start.
It is possible to call malloc in new bad it is really bad idea. It is possible to call new(std::nothrow) but it is bad too.

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

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

发布评论

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

评论(1

聊慰 2024-12-29 06:29:55

你不需要保存指针;定义一个放置新操作符(任何
operator new 函数需要多个参数)则不会
删除现有的操作员;它只会让他们超负荷。要致电
布局运算符新函数中的标准运算符新函数:

p = ::operator new( size );

请注意,如果布局新返回,您不想执行此操作
除了 ::operator new 返回的确切地址之外的任何内容。否则,
删除不起作用。如果您使用任何返回任何内容的新展示位置
除了 ::operator new 返回的指针之外,您还需要定义一个
新的全局操作符删除(因为这是将要删除的操作符)
称为),这又意味着你必须定义一个新的标准
operator new 也是如此,这样它就可以与您的 new 一起正常工作
运算符删除。为了避免必须实施所有
自己管理内存,在你的程序中使用mallocfree
实施。

You don't need to save pointers; defining a placement new operator (any
operator new function which takes more than one argument) doesn't
remove the existing operators; it just overloads them. To call the
standard operator new function from a placement operator new function:

p = ::operator new( size );

Note that you do not want to do this if your placement new returns
anything but the exact address returned by ::operator new. Otherwise,
delete won't work. If you use any placement new that return anything
other than a pointer returned by ::operator new, you need to define a
new global operator delete (because this is the one that will be
called), which in turn means that you have to define a new standard
operator new as well, so that it will work correctly with your new
operator delete. In order to avoid having to implement all of the
memory management yourself, use malloc and free in your
implementations.

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