析构函数,不会删除我的对象

Mat*_*man -1 c++ destructor linked-list list

我有很大的问题 - 即我的析构函数不删除对象,在我的代码中,当我调用l3.~list();它时,我将在main下面粘贴它只删除单链表(这是好的),但它不会删除char*name,甚至虽然我在说我的析构函数delete [] name;.任何想法都错了吗?

这是代码;

#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;

class list{

    struct lista
    {
        int num;
        char* word;
        lista* next;
    };
    lista* head;
    char* name;
    public:
    list(char* name1){head=NULL;name=new char[strlen(name1)+1];strcpy(name,name1);}
    char getChar(int key, int index);
    void setChar(int key, int index, char c);
    void insert(int number,char* txt);
    void remove(int number);
    void print();
    list(const list &o);
    list& operator=(const list &x);
    ~list();
};
void list::insert(int number,char* txt){
    lista* ptr,*tmp;
    ptr=head;
    lista* newlista=new lista;
    newlista->num=number;
    newlista->next=NULL;
    newlista->word= new char[strlen(txt)+1];
    strcpy(newlista->word,txt);
    if(head==NULL){
        head=newlista;
        newlista->next=NULL;
    }
    else while(ptr!=NULL){
        if(strcmp(txt,ptr->word)>=0){
            if(ptr->next!=NULL && strcmp(txt,ptr->next->word)<=0)
            {
                tmp=ptr->next;
                ptr->next=newlista;
                newlista->next=tmp;
                break;
            }
            else if(ptr->next!=NULL && strcmp(txt,ptr->next->word)>0)
                ptr=ptr->next;
            else
            {
                //next is empty
                ptr->next=newlista;
                break;
            }
        }
        else{
            //txt mniejszy niz w 1szym elemencie
            newlista->next=head;
            head=newlista;
            break;
        }      
    }
    return;
}

void list::print(){
    cout<<name<<";"<<endl;
    lista *druk;
    druk=head;
    while(druk!=NULL){
        cout<<"txt: "<<druk->word<<" | "<<"num: "<<druk->num<<endl;
        druk=druk->next;
    }
    cout<<endl;
    return;
}


void list::remove(int number){
    if(head==NULL)
        return;
    if(head->num==number){
        lista* ptr=head;
        head=head->next;
        delete [] ptr->word;
        delete ptr;
        return;
    }
    lista* ptr=head;
    while(ptr->next!=NULL && ptr->next->num!=number)
        ptr=ptr->next;
    if(ptr->next==NULL){
        cout<<number<<" element not found"<<endl;
        return;
    }
    lista* todelete=ptr->next;
    ptr->next=todelete->next;
    delete [] todelete->word;
    delete todelete;
    return;
}
list::list(const list &o)
{
    lista *xtr = o.head;
    head=NULL;// bez tego nie dzia?a
    lista *etr=head;// nastawic etr na head?
    while (xtr)
    {
        lista* ntr = new lista;
        if (!ntr)
        {
            cerr << "list::CopyConstructor: Allocation memory failure!";
            cerr << endl;
            break;
        }
        ntr->num = xtr->num;
        ntr->word= new char[strlen(xtr->word)+1];
        strcpy(ntr->word,xtr->word);
        ntr->next = NULL;
        if (head)
            etr->next = ntr;    
        else
            head = ntr;
        etr = ntr; // keep track of the last element in *this
        xtr = xtr->next;
    }
    name = new char[strlen(o.name)+5];
    strcpy(name,o.name);
    strcat(name,"Copy");
}

list& list::operator=(const list &x)
{
    if(this==&x)
        return *this;
    lista *etr=head;
    while(etr) // removing list from this
    {
        etr=etr->next;
        delete head;
        head=etr;
    }
    lista *xtr=x.head;
    while(xtr)
    {
        int copied=xtr->num;
        lista *ntr= new lista;
        ntr->word=new char[strlen(xtr->word)+1];
        if (!ntr) 
        {
            cerr << "list::operator=: Allocation memory failure!" << endl;
            break;
        }
        ntr->num=copied;
        strcpy(ntr->word,xtr->word);
        ntr->next=NULL;
        if (!head)
            head = ntr;
        else
            etr->next = ntr;

        etr = ntr; // keep track of the last element in *this
        xtr = xtr->next;
    }
    char *name=new char[strlen(x.name)+1];
    strcpy(name,x.name);
    return *this;
}

list::~list()
{
    cout<<"Object with name:"<<name<<" destroyed!"<<endl;
    delete [] name;
    lista *dtr=head;
    while(dtr) // removing lista from this
    {
        dtr=dtr->next;
        delete [] head->word;
        delete head;
        head=dtr;
    }

}
void f();
void f(){
    list o("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
    o.insert(4,"kazio");
    o.insert(100,"312jh31io2");
    o.insert(34,"kz31231azio");
    o.insert(1020,"123213312jh31io2");
    o.insert(213123,"z3213io");
    o.insert(1100,"zdrf312jh31io2");
    o.print();
}
int main(){
    list l1("lista1");
    l1.insert(5,"Endian");
    l1.insert(7,"Endianness");
    l1.insert(100,"Hexediting");
    l1.insert(34,".mil");

    l1.print();
    list l2(l1); // usage of CC - the same as list l2=l1; 
    l2.print();
    l2.remove(5);
    l2.print();
    l1.print();

    list l3("asajnment");
    l3=l2=l1;
    l3.print();
    l2.print();
    f();
    l3.print();
    l3.~list(); // here i use destructor on l3
    l3.print(); // l3 is being printed with weird name, even though it should be destroyed
    getchar();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

Raf*_*cki 5

在调用析构函数之后调用任何方法会导致未定义的行为 - 它可能也可能不起作用,它可能会产生奇怪的结果.

此外,您不应该直接调用析构函数:

  • 当对象在堆栈上分配时,它会在作用域结束时自动销毁.(范围是大括号之间的东西{})
  • 当在堆上分配对象时,使用new它,它应该被销毁delete.

  • 此外,您不能直接在具有静态存储持续时间的对象上调用析构函数(即声明在函数和类外部,或声明为`static`); 这些程序在程序退出时会自动销毁.你直接调用它的唯一时间是使用放置`new`创建对象. (2认同)