析构函数默默地做什么?

发布于 2024-10-22 02:12:23 字数 3574 浏览 2 评论 0原文

考虑到下面的代码看起来析构函数没有做任何实际工作,valgrind 清楚地向我表明它在不使用析构函数的情况下存在内存泄漏。任何人都可以解释一下析构函数在这种情况下做了什么?

#include <iostream>                                                                    
using namespace std;                                                                   

class A                                                                                
{                                                                                      
private:                                                                               
    int value;                                                                         
    A* follower;                                                                       
public:                                                                                
    A(int);                                                                            
    ~A();                                                                        
    void insert(int);                                                                  
};                                                                                     

A::A(int n)                                                                            
{                                                                                      
    value = n;                                                                         
    follower = NULL;                                                                   
}                                                                                      

A::~A()                                                                             
{                                                                                   
     if (follower != NULL)                                                           
         delete follower;                                                               
     cout << "do nothing!" << endl;                                             
}                                                                                   

void A::insert(int n)                                                                  
{                                                                                      
    if (this->follower == NULL) {                                                      
        A* f = new A(n);                                                               
        this->follower = f;                                                            
    }                                                                                  
    else                                                                               
        this->follower->insert(n);                                                     
}                                                                                      

int main(int argc, char* argv[])                                                       
{                                                                                      
    A* objectA = new A(1);                                                             

    int i;                                                                             
    for (i = 0; i < 10; i++)                                                           
        objectA->insert(i);                                                            

    delete objectA;                                                                    
} 

Considering the following code which looks like that the destructor doesn't do any real job, valgrind showed me clearly that it has memory leak without using the destructor. Any body can explain me what does the destructor do in this case?

#include <iostream>                                                                    
using namespace std;                                                                   

class A                                                                                
{                                                                                      
private:                                                                               
    int value;                                                                         
    A* follower;                                                                       
public:                                                                                
    A(int);                                                                            
    ~A();                                                                        
    void insert(int);                                                                  
};                                                                                     

A::A(int n)                                                                            
{                                                                                      
    value = n;                                                                         
    follower = NULL;                                                                   
}                                                                                      

A::~A()                                                                             
{                                                                                   
     if (follower != NULL)                                                           
         delete follower;                                                               
     cout << "do nothing!" << endl;                                             
}                                                                                   

void A::insert(int n)                                                                  
{                                                                                      
    if (this->follower == NULL) {                                                      
        A* f = new A(n);                                                               
        this->follower = f;                                                            
    }                                                                                  
    else                                                                               
        this->follower->insert(n);                                                     
}                                                                                      

int main(int argc, char* argv[])                                                       
{                                                                                      
    A* objectA = new A(1);                                                             

    int i;                                                                             
    for (i = 0; i < 10; i++)                                                           
        objectA->insert(i);                                                            

    delete objectA;                                                                    
} 

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

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

发布评论

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

评论(3

一杯敬自由 2024-10-29 02:12:23

insert() 方法在堆上创建新的 A 并使用跟随者指针将其链接到下一个元素。因此,如果 A 上没有析构函数,follower 指向的对象永远不会被删除。

The insert() method creates new A on the heap and links it to the next element with the follower pointer. So without the destructor on A, the object pointed to by follower never gets deleted.

铁轨上的流浪者 2024-10-29 02:12:23

首先,我们希望这是为了练习,否则 std::forward_list 似乎更合适(并且 vector 可能会更好)。

其次,析构函数的作用简单来说就是执行你放入其中的代码,这里的作用是释放follower获取的资源。

delete follower的调用做了两件事:

  • 调用follower的析构函数
  • ,然后释放follower所在的内存> 被存储

如果没有调用delete,就会出现内存泄漏。

注意:

  • 您的类缺少复制构造函数和赋值运算符
  • main 中,没有必要new A 的实例。

First, let us hope this is for exercise, otherwise a std::forward_list<int> seems much more adequate (and a vector<int> would probably be better).

Second, the destructor role is, simply, to execute the code you put in it, which here is about releasing the resources acquired by follower.

The call to delete follower does two things:

  • it calls the destructor of follower
  • it then release the memory at which follower was stored

Without the call to delete, you have a memory leak.

Notes:

  • Your class lacks a copy constructor and assignment operator
  • It is not necessary, in main, to new the instance of A.
浅黛梨妆こ 2024-10-29 02:12:23

这段代码看起来析构函数确实做了真正的工作:它删除了跟随者指针,该指针调用 ~A(),而 ~A() 又将删除其跟随者,依此类推。

如果省略析构函数,所有分配的节点将不会被删除。

This code looks like the destructor does do real job: it deletes follower pointer, which calls ~A(), which, in turn will delete its follower and so on.

If you omit the destructor all allocated nodes will not be deleted.

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