Note that there are some explanatory texts on larger screens.

plurals
  1. PODestructor, doesn't delete my object
    text
    copied!<p>I have big problem- namely my destructor doesn't delete object, in my code which i will paste underneath in main when i call <code>l3.~list();</code> it removes only singly linked list(which is good), but it doesn't remove char* name, even though I am stating in my destructor <code>delete [] name;</code>. Any ideas whats wrong?</p> <p>Here is the code;</p> <pre><code>#include &lt;iostream&gt; #include &lt;cstdlib&gt; #include &lt;string&gt; 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 &amp;o); list&amp; operator=(const list &amp;x); ~list(); }; void list::insert(int number,char* txt){ lista* ptr,*tmp; ptr=head; lista* newlista=new lista; newlista-&gt;num=number; newlista-&gt;next=NULL; newlista-&gt;word= new char[strlen(txt)+1]; strcpy(newlista-&gt;word,txt); if(head==NULL){ head=newlista; newlista-&gt;next=NULL; } else while(ptr!=NULL){ if(strcmp(txt,ptr-&gt;word)&gt;=0){ if(ptr-&gt;next!=NULL &amp;&amp; strcmp(txt,ptr-&gt;next-&gt;word)&lt;=0) { tmp=ptr-&gt;next; ptr-&gt;next=newlista; newlista-&gt;next=tmp; break; } else if(ptr-&gt;next!=NULL &amp;&amp; strcmp(txt,ptr-&gt;next-&gt;word)&gt;0) ptr=ptr-&gt;next; else { //next is empty ptr-&gt;next=newlista; break; } } else{ //txt mniejszy niz w 1szym elemencie newlista-&gt;next=head; head=newlista; break; } } return; } void list::print(){ cout&lt;&lt;name&lt;&lt;";"&lt;&lt;endl; lista *druk; druk=head; while(druk!=NULL){ cout&lt;&lt;"txt: "&lt;&lt;druk-&gt;word&lt;&lt;" | "&lt;&lt;"num: "&lt;&lt;druk-&gt;num&lt;&lt;endl; druk=druk-&gt;next; } cout&lt;&lt;endl; return; } void list::remove(int number){ if(head==NULL) return; if(head-&gt;num==number){ lista* ptr=head; head=head-&gt;next; delete [] ptr-&gt;word; delete ptr; return; } lista* ptr=head; while(ptr-&gt;next!=NULL &amp;&amp; ptr-&gt;next-&gt;num!=number) ptr=ptr-&gt;next; if(ptr-&gt;next==NULL){ cout&lt;&lt;number&lt;&lt;" element not found"&lt;&lt;endl; return; } lista* todelete=ptr-&gt;next; ptr-&gt;next=todelete-&gt;next; delete [] todelete-&gt;word; delete todelete; return; } list::list(const list &amp;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 &lt;&lt; "list::CopyConstructor: Allocation memory failure!"; cerr &lt;&lt; endl; break; } ntr-&gt;num = xtr-&gt;num; ntr-&gt;word= new char[strlen(xtr-&gt;word)+1]; strcpy(ntr-&gt;word,xtr-&gt;word); ntr-&gt;next = NULL; if (head) etr-&gt;next = ntr; else head = ntr; etr = ntr; // keep track of the last element in *this xtr = xtr-&gt;next; } name = new char[strlen(o.name)+5]; strcpy(name,o.name); strcat(name,"Copy"); } list&amp; list::operator=(const list &amp;x) { if(this==&amp;x) return *this; lista *etr=head; while(etr) // removing list from this { etr=etr-&gt;next; delete head; head=etr; } lista *xtr=x.head; while(xtr) { int copied=xtr-&gt;num; lista *ntr= new lista; ntr-&gt;word=new char[strlen(xtr-&gt;word)+1]; if (!ntr) { cerr &lt;&lt; "list::operator=: Allocation memory failure!" &lt;&lt; endl; break; } ntr-&gt;num=copied; strcpy(ntr-&gt;word,xtr-&gt;word); ntr-&gt;next=NULL; if (!head) head = ntr; else etr-&gt;next = ntr; etr = ntr; // keep track of the last element in *this xtr = xtr-&gt;next; } char *name=new char[strlen(x.name)+1]; strcpy(name,x.name); return *this; } list::~list() { cout&lt;&lt;"Object with name:"&lt;&lt;name&lt;&lt;" destroyed!"&lt;&lt;endl; delete [] name; lista *dtr=head; while(dtr) // removing lista from this { dtr=dtr-&gt;next; delete [] head-&gt;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; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload