写入析构函数时,删除NULL指针不会调用重载的删除 [英] delete a NULL pointer does not call overloaded delete when destructor is written

查看:197
本文介绍了写入析构函数时,删除NULL指针不会调用重载的删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class Widget
{
    public:
        Widget() {
            cout<<"~Widget()"<<endl;
        }
        ~Widget() {
            cout<<"~Widget()"<<endl;
        }

    void* operator new(size_t sz) throw(bad_alloc) {
        cout<<"operator new"<<endl;
        throw bad_alloc();
    }

    void operator delete(void *v) {
        cout<<"operator delete"<<endl;
    }

};

int main() 
{
    Widget* w = 0;
    try {
        w = new Widget();
    }
    catch(bad_alloc) {
        cout<<"Out of Memory"<<endl;
    }

    delete w;
    getch();
    return 1;
}

在此代码中,当析构函数在其中时,delete w不会调用重载的delete运算符.如果省略析构函数,则调用重载的delete.为什么会这样?

In this code, delete w does not call the overloaded delete operator when the destructor is there. If the destructor is omitted, the overloaded delete is called. Why is this so?

写入〜Widget()时的输出

操作员新
内存不足

operator new
Out of Memory

未编写〜Widget()时的输出

操作员新
内存不足
运算符删除

operator new
Out of Memory
operator delete

推荐答案

我记得前一段时间在comp.lang.c ++.moderated中,对运算符进行了类似的删除.我现在找不到它,但是答案表明是这样的..

I remember something similar on operator delete a while ago in comp.lang.c++.moderated. I cannot find it now, but the answer stated something like this ..

不幸的是,语言 规格不够 明确是否应进行控制 进入超载的操作员删除" 调用delete-expression时 在相应的空指针上 类型,即使标准确实 说在上的delete-expression 空指针是空操作.

Unfortunately, the language specification is not sufficiently clear on whether the control should go into the overloaded 'operator delete' when the delete-expression is invoked on the null-pointer of corresponding type, even though the standard does say that delete-expression on null-pointer is a no-op.

詹姆斯·坎泽(James Kanze)专门说:

这仍然是 运算符delete(或delete []) 查看;标准不保证 它不会被赋予空指针; 该标准要求它必须是 如果给出空指针,则不进行操作.或者那个 该实现被允许调用 它.根据最新草案, 第一个论点的价值 提供给解除分配功能 可以是空指针值;如果是这样, 并且如果解除分配函数是 标准库中提供的一个 通话没有任何效果." 肯定那是什么意思 标准库中提供的一个" 从字面上看, 因为他的功能没有提供 由标准库,句子 似乎不适用.但是不知何故 没道理

It's still the responisiblity of operator delete (or delete[]) to check; the standard doesn't guarantee that it won't be given a null pointer; the standard requires that it be a no-op if given a null pointer. Or that the implementation is allowed to call it. According to the latest draft, "The value of the first argument supplied to a deallocation function may be a null pointer value; if so, and if the deallocation function is one supplied in the standard library, the call has no effect." I'm not quite sure what the implications of that "is one supplied in the standard library" are meant to be---taken literally, since his function is not one provided by the standard library, the sentence wouldn't seem to apply. But somehow, that doesn't make sense

我记得这个becoz有时也有类似的问题,并将答案保存在.txt文件中.

I remember this becoz i had a similar prob sometime back and had preserved the answer in a .txt file.

UPDATE-1:

哦,我在此处找到了它. 另请阅读此链接缺陷报告. 因此,答案是 未指定 . 5.3.5/7 一章.

Oh i found it here. Also read this link defect report. So, the answer is Unspecified. Chapter 5.3.5/7.

这篇关于写入析构函数时,删除NULL指针不会调用重载的删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆