双重删除数据不会崩溃 [英] Double delete of data doesn't crash

查看:56
本文介绍了双重删除数据不会崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习C ++,并且正在编写程序来学习复制构造函数和运算符重载.我感到惊讶的是,使用Copy构造函数时,以下程序不会崩溃并显示"Double Free",而使用Operator =重载时,崩溃始终会崩溃.

I am trying to learn C++ and was writing programs to learn Copy Constructors and Operator overloading. I am surprised that the below program when Copy constructor is used doesn't crash saying "Double Free", whereas when using Operator = overloading crashes consistently.

#include <iostream>
using namespace std;

class XHandler
{
    public:
    XHandler()
    {
            data = new char[8];
            strcpy(data, "NoName");
    }
    XHandler (const char *str)
    {
            data = new char (strlen(str) + 1 );
            strcpy (data, str);
    }
    XHandler (const XHandler &xh)
    {
            data = xh.data;
    }
    XHandler& operator = (const XHandler &xh)
    {
            data = xh.data;
    }
    ~XHandler()
    {
            delete data;
    }
    void debug()
    {
            cout << data <<endl;
    }
    private:
    char *data;
};

int main()
{
    XHandler wm("hello"), wb("there");
    wm.debug();
    wb.debug();
    XHandler wc (wm);
    wc.debug();
    XHandler wd;
    wd = wc;
    wd.debug();
}

请注意,在复制构造函数和运算符重载中,我只是将数据"指针从一个对象复制到另一个对象.当为"wd"和"wc"调用析构函数时,它将一致地使程序崩溃.如果我注释以下各行并仅执行复制构造函数,则该程序完全不会崩溃.我希望,由于'wc'和'wm'的数据变量也都指向相同的指针,因此程序将崩溃.

Note that in both copy constructor and operator overloading, I am just copying the 'data' pointer from one object to another. When the destructor is invoked for 'wd' and 'wc', it crashes the program consistently. If I comment the below lines and execute only the copy constructor, the program doesn't crash at all. I would expect since both 'wc's and 'wm's data variable are also pointing to the same pointer, the program would crash.

    XHandler wd;
    wd = wc;
    wd.debug();

我知道重复删除是不确定的行为.但是,我想知道的是它总是以一种方式崩溃,而没有以另一种方式崩溃.

I understand double delete is undefined behaviour. But, what I wonder is it consistently crashes in one way and doesn't in the other.

推荐答案

未定义行为"的行为只是未定义.这意味着,没有人会为这种情况而努力,因为这种情况不应该发生.甚至一个正在运行的程序也可能是未定义的行为".

The behavior of "Undefined behavior" is just undefined. That means, no one put effort into that cases because that cases should not happen. Even a working program can be "Undefined behavior".

在双重删除的特殊情况下,发生的事情实际上取决于分配器的实现.通常的实现是将已释放的内存放入列表中,并使用该列表之外的元素满足下一个分配.如果您两次删除一块内存,它将两次被添加到列表中.然后,将在同一块内存中满足两个分配,因此将在相同的内存位置创建两个对象.

In your special case of an double delete, what happens, really depends on the implementation of the allocator. A usual implementation is to put released memory into a list and a next allocation will be satisfied with an element out of this list. If you double delete a chunk of memory, it will be added to the list twice. And then two allocation will be satisfied with the very same chunk of memory and thus two object will be created at the same memory location.

顺便说一句:您的析构函数已损坏,因为它不使用数组删除.

BTW: You destructor is already broken, as it doesn't use the array delete.

这篇关于双重删除数据不会崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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