Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your linked list looks something like this:</p> <pre class="lang-none prettyprint-override"><code>+------+----+----+ | Head | hd | tl | ----------&gt;-------- +------+----+----+ \ | ----&gt;------ | NULL | / \ | | +------+-----+------+------+ +------+-----+------+------+ | Node | x=0 | next | prev | | Node | x=1 | next | prev | +------+-----+------+------+ +------+-----+------+------+ | | | \ NULL / -----------------------&lt;---------------------- </code></pre> <p>(I've simplified it to two nodes).</p> <p>Now, we can just write out what this code does:</p> <pre><code>linkNode = hdd-&gt;hd; while(linkNode!=0) { free(linkNode-&gt;prev); linkNode = linkNode-&gt;next; } </code></pre> <ol> <li><code>linkNode = hdd-&gt;hd</code> leaves <code>linkNode</code> pointed at the first node</li> <li><code>(linkNode!=0)</code> is true (the first node is not NULL), so we enter the while loop</li> <li><code>free(linkNode-&gt;prev)</code> calls <code>free(NULL)</code> since <code>hdd-&gt;hd-&gt;prev == NULL</code> (you set the first node up like this explicitly). This is fine, but does nothing.</li> <li><code>linkNode = linkNode-&gt;next</code> leaves <code>linkNode</code> pointed at the last node</li> <li><code>linkNode!=0</code> is still true (the last node is also not NULL), so we go round the loop again</li> <li><code>free(linkNode-&gt;prev)</code> frees the previous node (which is the first one)</li> <li><code>linkNode = linkNode-&gt;next</code> leaves <code>linkNode == NULL</code></li> <li><code>linkNode!=0</code> is false now, so the loop terminates.</li> </ol> <p>So, we free'd all but the last node. No node's <code>prev</code> member points to that node, so calling <code>free(linkNode-&gt;prev)</code> can never free it. You could, however, free it via <code>hdd-&gt;tl</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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