简体   繁体   English

C++ 动态内存 // 销毁指针指向的内存

[英]C++ Dynamic Memory // Destroying memory pointed by pointer

#include <iostream>

using namespace std;

#define SELECT 0

class Z
{
    private:
        int *z1; int *z2;
    public:
        Z(const int x1 = 0, const int x2 = 0);
        Z(const Z &X);
        int *first (void) const {return z1;};
        int *second (void) const {return z2;};
        ~Z(void);
};

Z::Z(const int x1,const int x2){
    z1 = new int(x1);
    z2 = new int(x2);
}

#if SELECT == 1
Z::Z(const Z &X){
    z1 = new int(*X.first() );
    z2 = new int(*X.second() );
}
#else
Z::Z(const Z &X){
    z1 = X.first();
    z2 = X.second();
}
#endif

Z::~Z(){
    delete z1;
    delete z2;
}

int main()
{
 Z *zp;
    zp = new Z(3,5);
    Z  c(0,0);
    c = *zp;
    cout << "Content of c: " << *c.first() << " and " << *c.second() << endl;
    delete zp;
    cout << "Content of c: " << *c.first() << " and " << *c.second() << endl;
}

Hello, when I run this code, I get something like你好,当我运行这段代码时,我得到类似的信息

Content of c: 3 and 5
Content of c: Garbage and Garbage

I was expecting this since, I am not creating another memory for c instead it points contents of zp.我期待这一点,因为我没有为 c 创建另一个内存,而是指向 zp 的内容。 However, when I switch #define SELECT 1, now I am creating new memory for c.但是,当我切换 #define SELECT 1 时,现在我正在为 c 创建新内存。 So when I delete zp, c still should point correct values (stored in different memory than zp) but what I get is still garbage shown below.所以当我删除 zp 时,c 仍然应该指向正确的值(存储在与 zp 不同的内存中)但我得到的仍然是下面显示的垃圾。

Content of c: 3 and 5
Content of c: Garbage and Garbage

Where is the problem?问题出在哪儿?

I have another question.我有另一个问题。 When I debug this in VScode, I get "Exception Occured. Unknown signal "for the lines当我在 VScode 中调试这个时,我得到“异常发生。未知信号”的行

    delete z1;
    delete z2;

In CodeBlocks, there is no error.在 CodeBlocks 中,没有错误。 What is the problem?问题是什么?

Thanks for help.感谢帮助。

You don't follow the rule of three 1 2 .你没有遵循三个1 2的规则。

The copy constructor is only one part of the rule of three, but you also have to implement the copy assignment operator.复制构造函数只是三者规则的一部分,但您还必须实现复制赋值运算符。

The c = *zp; c = *zp; does not call the copy constructor but the copy assignment operator, which is in your case the default one because you didn't specify any.不调用复制构造函数,而是调用复制赋值运算符,在您的情况下,这是默认的,因为您没有指定任何。

The default copy assignment operator will copy the pointers, so after c = *zp;默认的复制赋值运算符会复制指针,所以在c = *zp; both c and zp have the same pointers for z1 and z2 which will lead to a double free (that's most certainly the reason for your last question) of the memory initially allocated by zp and memory leaking for the memory allocated by c . czpz1z2具有相同的指针,这将导致最初由zp分配的内存的双重释放(这肯定是你最后一个问题的原因)和由c分配的内存泄漏。

Z::Z(const Z &X){
    z1 = new int(*X.first() );
    z2 = new int(*X.second() );
}

Z& operator=(const Z& X)
{
  *z1 = *(X.z1);
  *z2 = *(X.z2);

  return *this;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM