Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy there's no error when I delete twice the same memory?
    text
    copied!<p>MySinglyLinkedList.h:</p> <pre><code>#include &lt;iostream&gt; template&lt;class T&gt; class LinkedList; template&lt;class T&gt; class LinkedNode { public: LinkedNode(T new_data):data(new_data) {; } private: friend class LinkedList&lt;T&gt;; LinkedNode&lt;T&gt; *next; T data; }; template&lt;class T&gt; class LinkedList { public: LinkedList(); ~LinkedList(); void PushNode(T new_data); void Delete(LinkedNode&lt;T&gt; *pnode); void Show(); private: LinkedNode&lt;T&gt; *head; //Head pointer LinkedNode&lt;T&gt; *tail; //Tail pointer int length; //Length of the list }; //Initialize an empty list when creating it template&lt;class T&gt; LinkedList&lt;T&gt;::LinkedList() { head = tail = NULL; length = 0; } //delete all the nodes when deconstructing the object template&lt;class T&gt; LinkedList&lt;T&gt;::~LinkedList() { LinkedNode&lt;T&gt; *ptr = head; while (ptr) { LinkedNode&lt;T&gt; *ptr_del = ptr; ptr = ptr-&gt;next; Delete(ptr_del); } } //Add one node to the tail of the list template&lt;class T&gt; void LinkedList&lt;T&gt;::PushNode(T new_data) { LinkedNode&lt;T&gt; *pnew_node = new LinkedNode&lt;T&gt;(new_data); pnew_node-&gt;next = NULL; if (!length) { head = tail = pnew_node; length++; } else { tail-&gt;next = pnew_node; tail = pnew_node; length++; } } //Delete the node pointed by pnode template&lt;class T&gt; void LinkedList&lt;T&gt;::Delete(LinkedNode&lt;T&gt; *pnode) { LinkedNode&lt;T&gt; *ptr = head; if (pnode==head) { head = pnode-&gt;next; } else { while(ptr-&gt;next != pnode) { ptr = ptr-&gt;next; } ptr-&gt;next = pnode-&gt;next; } if(pnode == tail) tail = ptr; delete pnode; length--; } template&lt;class T&gt; void LinkedList&lt;T&gt;::Show() //Print all the contents in the list { LinkedNode&lt;T&gt; *pnode = head; while(pnode) { std::cout &lt;&lt; pnode-&gt;data &lt;&lt; std::endl; pnode = pnode-&gt;next; } std::cout &lt;&lt; "In total: " &lt;&lt; length &lt;&lt; std::endl; } </code></pre> <p>The main function is as follows:</p> <pre><code>#include "MySinglyLinkedList.h" #include &lt;cstdlib&gt; #include &lt;ctime&gt; using namespace std; int main(int argc, char *argv[]) { //list_len is the length of the list int list_len = 5; srand(time(0)); if (argc &gt; 1) list_len = atoi(argv[1]); LinkedList&lt;int&gt; test_list; //Create the first list: test_list for (int i = 0; i &lt; list_len; i++) { //The elements in the list are random integers int cur_data = rand()%list_len; test_list.PushNode(cur_data); } test_list.Show(); LinkedList&lt;int&gt; test2 = test_list; //Create the second list: test2 test2.Show(); return 0; } </code></pre> <p>Since I didn't define any copy constructor here, the default copy constructor will be called and do the bit copy when creating the second list, and as a result <code>test_list</code> and <code>test2</code> will point to the same linked list. Therefore the first node of the list will be deleted twice when the two object are deconstructed. But the fact is nothing wrong happened when I compiled the program using GCC, and it ran successfully in Linux. <strong>I don't understand why no error occurred.</strong></p>
 

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