Skip to content

Instantly share code, notes, and snippets.

@JoergWMittag
Created May 24, 2017 17:00
Show Gist options
  • Save JoergWMittag/e7d898310274be5833a572a6bea6bcde to your computer and use it in GitHub Desktop.
Save JoergWMittag/e7d898310274be5833a572a6bea6bcde to your computer and use it in GitHub Desktop.
<!doctype html>
<html>
<head>
<title>list.c</title>
<style type="text/css">
body { color:#000000; background-color:#ffffff }
body { font-family:Helvetica, sans-serif; font-size:10pt }
h1 { font-size:14pt }
.code { border-collapse:collapse; width:100%; }
.code { font-family: "Monospace", monospace; font-size:10pt }
.code { line-height: 1.2em }
.comment { color: green; font-style: oblique }
.keyword { color: blue }
.string_literal { color: red }
.directive { color: darkmagenta }
.expansion { display: none; }
.macro:hover .expansion { display: block; border: 2px solid #FF0000; padding: 2px; background-color:#FFF0F0; font-weight: normal; -webkit-border-radius:5px; -webkit-box-shadow:1px 1px 7px #000; border-radius:5px; box-shadow:1px 1px 7px #000; position: absolute; top: -1em; left:10em; z-index: 1 }
.macro { color: darkmagenta; background-color:LemonChiffon; position: relative }
.num { width:2.5em; padding-right:2ex; background-color:#eeeeee }
.num { text-align:right; font-size:8pt }
.num { color:#444444 }
.line { padding-left: 1ex; border-left: 3px solid #ccc }
.line { white-space: pre }
.msg { -webkit-box-shadow:1px 1px 7px #000 }
.msg { box-shadow:1px 1px 7px #000 }
.msg { -webkit-border-radius:5px }
.msg { border-radius:5px }
.msg { font-family:Helvetica, sans-serif; font-size:8pt }
.msg { float:left }
.msg { padding:0.25em 1ex 0.25em 1ex }
.msg { margin-top:10px; margin-bottom:10px }
.msg { font-weight:bold }
.msg { max-width:60em; word-wrap: break-word; white-space: pre-wrap }
.msgT { padding:0x; spacing:0x }
.msgEvent { background-color:#fff8b4; color:#000000 }
.msgControl { background-color:#bbbbbb; color:#000000 }
.msgNote { background-color:#ddeeff; color:#000000 }
.mrange { background-color:#dfddf3 }
.mrange { border-bottom:1px solid #6F9DBE }
.PathIndex { font-weight: bold; padding:0px 5px; margin-right:5px; }
.PathIndex { -webkit-border-radius:8px }
.PathIndex { border-radius:8px }
.PathIndexEvent { background-color:#bfba87 }
.PathIndexControl { background-color:#8c8c8c }
.PathNav a { text-decoration:none; font-size: larger }
.CodeInsertionHint { font-weight: bold; background-color: #10dd10 }
.CodeRemovalHint { background-color:#de1010 }
.CodeRemovalHint { border-bottom:1px solid #6F9DBE }
table.simpletable {
padding: 5px;
font-size:12pt;
margin:20px;
border-collapse: collapse; border-spacing: 0px;
}
td.rowname {
text-align: right;
vertical-align: top;
font-weight: bold;
color:#444444;
padding-right:2ex;
}
</style>
</head>
<body>
<!-- BUGDESC Use of memory after it is freed -->
<!-- BUGTYPE Use-after-free -->
<!-- BUGCATEGORY Memory Error -->
<!-- BUGFILE /Users/joerg/Documents/list.c -->
<!-- FILENAME list.c -->
<!-- FUNCTIONNAME remove_beginning -->
<!-- ISSUEHASHCONTENTOFLINEINCONTEXT 8a0ba3c8e65ecab74eae7211bc5c908f -->
<!-- BUGLINE 128 -->
<!-- BUGCOLUMN 19 -->
<!-- BUGPATHLENGTH 5 -->
<!-- BUGMETAEND -->
<!-- REPORTHEADER -->
<h3>Bug Summary</h3>
<table class="simpletable">
<tr><td class="rowname">File:</td><td>/Users/joerg/Documents/list.c</td></tr>
<tr><td class="rowname">Warning:</td><td><a href="#EndPath">line 128, column 19</a><br />Use of memory after it is freed</td></tr>
</table>
<!-- REPORTSUMMARYEXTRA -->
<h3>Annotated Source Code</h3>
<table class="code">
<tr><td class="num" id="LN1">1</td><td class="line"><span class='directive'>#include &lt;stdio.h&gt;</span></td></tr>
<tr><td class="num" id="LN2">2</td><td class="line"><span class='directive'>#include &lt;stdlib.h&gt;</span></td></tr>
<tr><td class="num" id="LN3">3</td><td class="line"><span class='directive'>#include &lt;string.h&gt;</span></td></tr>
<tr><td class="num" id="LN4">4</td><td class="line"> </td></tr>
<tr><td class="num" id="LN5">5</td><td class="line"><span class='comment'>/********** GLOBALS *******************************/</span></td></tr>
<tr><td class="num" id="LN6">6</td><td class="line"><span class='directive'>#define <span class='macro'>OK<span class='expansion'>0</span></span> 0</span></td></tr>
<tr><td class="num" id="LN7">7</td><td class="line"><span class='directive'>#define <span class='macro'>ERROR<span class='expansion'>-1</span></span> -1</span></td></tr>
<tr><td class="num" id="LN8">8</td><td class="line"> </td></tr>
<tr><td class="num" id="LN9">9</td><td class="line"><span class='comment'>/********** STRUCT AND TYPES DEFINTIONS ***********/</span></td></tr>
<tr><td class="num" id="LN10">10</td><td class="line"><span class='comment'>/* a node with key, data and reference to next node*/</span></td></tr>
<tr><td class="num" id="LN11">11</td><td class="line"><span class='keyword'>typedef</span> <span class='keyword'>struct</span> Node {</td></tr>
<tr><td class="num" id="LN12">12</td><td class="line"> <span class='keyword'>int</span> key;</td></tr>
<tr><td class="num" id="LN13">13</td><td class="line"> <span class='keyword'>char</span> string[1024];</td></tr>
<tr><td class="num" id="LN14">14</td><td class="line"> <span class='keyword'>struct</span> Node *next; <span class='comment'>// pointer to next node</span></td></tr>
<tr><td class="num" id="LN15">15</td><td class="line">} Node;</td></tr>
<tr><td class="num" id="LN16">16</td><td class="line"> </td></tr>
<tr><td class="num" id="LN17">17</td><td class="line"><span class='comment'>/* the actual linked list: ref to first and last Node, size attribute */</span></td></tr>
<tr><td class="num" id="LN18">18</td><td class="line"><span class='keyword'>typedef</span> <span class='keyword'>struct</span> LinkedList {</td></tr>
<tr><td class="num" id="LN19">19</td><td class="line"> <span class='keyword'>struct</span> Node *first;</td></tr>
<tr><td class="num" id="LN20">20</td><td class="line"> <span class='keyword'>struct</span> Node *last;</td></tr>
<tr><td class="num" id="LN21">21</td><td class="line"> <span class='keyword'>int</span> size;</td></tr>
<tr><td class="num" id="LN22">22</td><td class="line">} LinkedList;</td></tr>
<tr><td class="num" id="LN23">23</td><td class="line"> </td></tr>
<tr><td class="num" id="LN24">24</td><td class="line"><span class='comment'>/********** FUNCTION HEADERS **********************/</span></td></tr>
<tr><td class="num" id="LN25">25</td><td class="line">LinkedList* init_list();</td></tr>
<tr><td class="num" id="LN26">26</td><td class="line"><span class='keyword'>void</span> insert_end(LinkedList *list, <span class='keyword'>int</span> key, <span class='keyword'>char</span> string[]);</td></tr>
<tr><td class="num" id="LN27">27</td><td class="line"><span class='keyword'>void</span> insert_beginning(LinkedList *list, <span class='keyword'>int</span> key, <span class='keyword'>char</span> string[]);</td></tr>
<tr><td class="num" id="LN28">28</td><td class="line"><span class='keyword'>int</span> remove_end(LinkedList *list);</td></tr>
<tr><td class="num" id="LN29">29</td><td class="line"><span class='keyword'>int</span> remove_beginning(LinkedList *list);</td></tr>
<tr><td class="num" id="LN30">30</td><td class="line"><span class='keyword'>int</span> print_list(LinkedList *list);</td></tr>
<tr><td class="num" id="LN31">31</td><td class="line"><span class='keyword'>void</span> free_list(LinkedList *list);</td></tr>
<tr><td class="num" id="LN32">32</td><td class="line"><span class='keyword'>char</span> * get_string(LinkedList *list, <span class='keyword'>int</span> key);</td></tr>
<tr><td class="num" id="LN33">33</td><td class="line"> </td></tr>
<tr><td class="num" id="LN34">34</td><td class="line"><span class='comment'>/*********** FUNCTION DEFINITIONS ***************/</span></td></tr>
<tr><td class="num" id="LN35">35</td><td class="line"> </td></tr>
<tr><td class="num" id="LN36">36</td><td class="line"><span class='comment'>/**</span></td></tr>
<tr><td class="num" id="LN37">37</td><td class="line"> <span class='comment'>* init_list Returns an appropriately (for an empty list) initialized struct List</span></td></tr>
<tr><td class="num" id="LN38">38</td><td class="line"> <span class='comment'>*</span></td></tr>
<tr><td class="num" id="LN39">39</td><td class="line"> <span class='comment'>* @return LinkedList * ..ptr to the newly initialized list</span></td></tr>
<tr><td class="num" id="LN40">40</td><td class="line"> <span class='comment'>*/</span></td></tr>
<tr><td class="num" id="LN41">41</td><td class="line">LinkedList * init_list() {</td></tr>
<tr><td class="num" id="LN42">42</td><td class="line"> printf(<span class='string_literal'>"initializing list...\n"</span>);</td></tr>
<tr><td class="num" id="LN43">43</td><td class="line"> </td></tr>
<tr><td class="num" id="LN44">44</td><td class="line"> LinkedList *list = (LinkedList*) malloc(<span class='keyword'>sizeof</span>(LinkedList));</td></tr>
<tr><td class="num" id="LN45">45</td><td class="line"> </td></tr>
<tr><td class="num" id="LN46">46</td><td class="line"> list-&gt;first = <span class='macro'>NULL<span class='expansion'>((void *)0)</span></span>;</td></tr>
<tr><td class="num" id="LN47">47</td><td class="line"> list-&gt;last = <span class='macro'>NULL<span class='expansion'>((void *)0)</span></span>;</td></tr>
<tr><td class="num" id="LN48">48</td><td class="line"> list-&gt;size = 0;</td></tr>
<tr><td class="num" id="LN49">49</td><td class="line"> </td></tr>
<tr><td class="num" id="LN50">50</td><td class="line"> <span class='keyword'>return</span> list;</td></tr>
<tr><td class="num" id="LN51">51</td><td class="line">}</td></tr>
<tr><td class="num" id="LN52">52</td><td class="line"> </td></tr>
<tr><td class="num" id="LN53">53</td><td class="line"><span class='comment'>/**</span></td></tr>
<tr><td class="num" id="LN54">54</td><td class="line"> <span class='comment'>* Given a List, a key and a string adds a Node containing this</span></td></tr>
<tr><td class="num" id="LN55">55</td><td class="line"> <span class='comment'>* information at the end of the list</span></td></tr>
<tr><td class="num" id="LN56">56</td><td class="line"> <span class='comment'>*</span></td></tr>
<tr><td class="num" id="LN57">57</td><td class="line"> <span class='comment'>* @param list LinkedList * ..ptr to LinkedList</span></td></tr>
<tr><td class="num" id="LN58">58</td><td class="line"> <span class='comment'>* @param key int .. key of the Node to be inserted</span></td></tr>
<tr><td class="num" id="LN59">59</td><td class="line"> <span class='comment'>* @param string char[] .. the string of the Node to be inserted</span></td></tr>
<tr><td class="num" id="LN60">60</td><td class="line"> <span class='comment'>*/</span></td></tr>
<tr><td class="num" id="LN61">61</td><td class="line"><span class='keyword'>void</span> insert_end(LinkedList *list, <span class='keyword'>int</span> key, <span class='keyword'>char</span> string[]) {</td></tr>
<tr><td class="num" id="LN62">62</td><td class="line"> printf(<span class='string_literal'>"----------------------\n"</span>);</td></tr>
<tr><td class="num" id="LN63">63</td><td class="line"> </td></tr>
<tr><td class="num" id="LN64">64</td><td class="line"> list-&gt;size++; <span class='comment'>// increment size of list</span></td></tr>
<tr><td class="num" id="LN65">65</td><td class="line"> </td></tr>
<tr><td class="num" id="LN66">66</td><td class="line"> <span class='comment'>// intialize the new Node</span></td></tr>
<tr><td class="num" id="LN67">67</td><td class="line"> Node* newN = (Node*) malloc(<span class='keyword'>sizeof</span>(Node));</td></tr>
<tr><td class="num" id="LN68">68</td><td class="line"> newN-&gt;key = key;</td></tr>
<tr><td class="num" id="LN69">69</td><td class="line"> <span class='macro'>strcpy(newN-&gt;string, string)<span class='expansion'>__builtin___strcpy_chk (newN-&gt;string, string, __builtin_object_size<br> (newN-&gt;string, 2 &gt; 1 ? 1 : 0))</span></span>;</td></tr>
<tr><td class="num" id="LN70">70</td><td class="line"> newN-&gt;next = <span class='macro'>NULL<span class='expansion'>((void *)0)</span></span>;</td></tr>
<tr><td class="num" id="LN71">71</td><td class="line"> </td></tr>
<tr><td class="num" id="LN72">72</td><td class="line"> Node* oldLast = list-&gt;last; <span class='comment'>// get the old last</span></td></tr>
<tr><td class="num" id="LN73">73</td><td class="line"> oldLast-&gt;next = newN; <span class='comment'>// make new Node the next Node for oldlast</span></td></tr>
<tr><td class="num" id="LN74">74</td><td class="line"> list-&gt;last = newN; <span class='comment'>// set the new last in the list</span></td></tr>
<tr><td class="num" id="LN75">75</td><td class="line"> </td></tr>
<tr><td class="num" id="LN76">76</td><td class="line"> printf(<span class='string_literal'>"new Node(%p) at end: %d '%s' %p \n"</span>, newN, newN-&gt;key, newN-&gt;string,newN-&gt;next);</td></tr>
<tr><td class="num" id="LN77">77</td><td class="line">}</td></tr>
<tr><td class="num" id="LN78">78</td><td class="line"> </td></tr>
<tr><td class="num" id="LN79">79</td><td class="line"><span class='comment'>/**</span></td></tr>
<tr><td class="num" id="LN80">80</td><td class="line"> <span class='comment'>* Given a List, a key and a string adds a Node, containing</span></td></tr>
<tr><td class="num" id="LN81">81</td><td class="line"> <span class='comment'>* this information at the beginning of the list</span></td></tr>
<tr><td class="num" id="LN82">82</td><td class="line"> <span class='comment'>*</span></td></tr>
<tr><td class="num" id="LN83">83</td><td class="line"> <span class='comment'>* @param list LinkedList * ..ptr to LinkedList</span></td></tr>
<tr><td class="num" id="LN84">84</td><td class="line"> <span class='comment'>* @param key int .. key of the Node to be inserted</span></td></tr>
<tr><td class="num" id="LN85">85</td><td class="line"> <span class='comment'>* @param string char[] .. the string of the Node to be inserted</span></td></tr>
<tr><td class="num" id="LN86">86</td><td class="line"> <span class='comment'>*/</span></td></tr>
<tr><td class="num" id="LN87">87</td><td class="line"><span class='keyword'>void</span> insert_beginning(LinkedList *list, <span class='keyword'>int</span> key, <span class='keyword'>char</span> string[]) {</td></tr>
<tr><td class="num" id="LN88">88</td><td class="line"> printf(<span class='string_literal'>"----------------------\n"</span>);</td></tr>
<tr><td class="num" id="LN89">89</td><td class="line"> </td></tr>
<tr><td class="num" id="LN90">90</td><td class="line"> list-&gt;size++; <span class='comment'>// increment size of list</span></td></tr>
<tr><td class="num" id="LN91">91</td><td class="line"> Node* oldFirst = list-&gt;first; <span class='comment'>//get the old first node</span></td></tr>
<tr><td class="num" id="LN92">92</td><td class="line"> </td></tr>
<tr><td class="num" id="LN93">93</td><td class="line"> <span class='comment'>/* intialize the new Node */</span></td></tr>
<tr><td class="num" id="LN94">94</td><td class="line"> Node* newN = (Node*) malloc(<span class='keyword'>sizeof</span>(Node));</td></tr>
<tr><td class="num" id="LN95">95</td><td class="line"> newN-&gt;key = key;</td></tr>
<tr><td class="num" id="LN96">96</td><td class="line"> <span class='macro'>strcpy(newN-&gt;string, string)<span class='expansion'>__builtin___strcpy_chk (newN-&gt;string, string, __builtin_object_size<br> (newN-&gt;string, 2 &gt; 1 ? 1 : 0))</span></span>;</td></tr>
<tr><td class="num" id="LN97">97</td><td class="line"> newN-&gt;next = oldFirst;</td></tr>
<tr><td class="num" id="LN98">98</td><td class="line"> </td></tr>
<tr><td class="num" id="LN99">99</td><td class="line"> list-&gt;first = newN; <span class='comment'>// set the new first</span></td></tr>
<tr><td class="num" id="LN100">100</td><td class="line"> </td></tr>
<tr><td class="num" id="LN101">101</td><td class="line"> <span class='comment'>/* special case: if list size == 1, then this new one is also the last one */</span></td></tr>
<tr><td class="num" id="LN102">102</td><td class="line"> <span class='keyword'>if</span> (list-&gt;size == 1)</td></tr>
<tr><td class="num" id="LN103">103</td><td class="line"> list-&gt;last = newN;</td></tr>
<tr><td class="num" id="LN104">104</td><td class="line"> </td></tr>
<tr><td class="num" id="LN105">105</td><td class="line"> printf(<span class='string_literal'>"new Node(%p) at beginning: %d '%s' %p \n"</span>, newN, newN-&gt;key,newN-&gt;string, newN-&gt;next);</td></tr>
<tr><td class="num" id="LN106">106</td><td class="line">}</td></tr>
<tr><td class="num" id="LN107">107</td><td class="line"> </td></tr>
<tr><td class="num" id="LN108">108</td><td class="line"><span class='comment'>/**</span></td></tr>
<tr><td class="num" id="LN109">109</td><td class="line"> <span class='comment'>* Removes the first Node from the list</span></td></tr>
<tr><td class="num" id="LN110">110</td><td class="line"> <span class='comment'>*</span></td></tr>
<tr><td class="num" id="LN111">111</td><td class="line"> <span class='comment'>* @param list LinkedList * .. ptr to the List</span></td></tr>
<tr><td class="num" id="LN112">112</td><td class="line"> <span class='comment'>*</span></td></tr>
<tr><td class="num" id="LN113">113</td><td class="line"> <span class='comment'>* @return OK | ERROR</span></td></tr>
<tr><td class="num" id="LN114">114</td><td class="line"> <span class='comment'>*/</span></td></tr>
<tr><td class="num" id="LN115">115</td><td class="line"><span class='keyword'>int</span> remove_beginning(LinkedList *list) {</td></tr>
<tr><td class="num" id="LN116">116</td><td class="line"> printf(<span class='string_literal'>"----------------------\n"</span>);</td></tr>
<tr><td class="num" id="LN117">117</td><td class="line"> </td></tr>
<tr><td class="num" id="LN118">118</td><td class="line"> <span class='keyword'>if</span> (<span class="mrange">list-&gt;size &lt;= 0</span>)</td></tr>
<tr><td class="num"></td><td class="line"><div id="Path2" class="msg msgEvent" style="margin-left:9ex"><table class="msgT"><tr><td valign="top"><div class="PathIndex PathIndexEvent">2</div></td><td><div class="PathNav"><a href="#Path1" title="Previous event (1)">&#x2190;</a></div></td></td><td>Assuming the condition is false</td><td><div class="PathNav"><a href="#Path3" title="Next event (3)">&#x2192;</a></div></td></tr></table></div></td></tr>
<tr><td class="num"></td><td class="line"><div id="Path3" class="msg msgControl" style="margin-left:5ex"><table class="msgT"><tr><td valign="top"><div class="PathIndex PathIndexControl">3</div></td><td><div class="PathNav"><a href="#Path2" title="Previous event (2)">&#x2190;</a></div></td></td><td>Taking false branch</td><td><div class="PathNav"><a href="#Path4" title="Next event (4)">&#x2192;</a></div></td></tr></table></div></td></tr>
<tr><td class="num" id="LN119">119</td><td class="line"> <span class='keyword'>return</span> <span class='macro'>ERROR<span class='expansion'>-1</span></span>;</td></tr>
<tr><td class="num" id="LN120">120</td><td class="line"> </td></tr>
<tr><td class="num" id="LN121">121</td><td class="line"> list-&gt;size--;</td></tr>
<tr><td class="num" id="LN122">122</td><td class="line"> </td></tr>
<tr><td class="num" id="LN123">123</td><td class="line"> Node * oldFirst = list-&gt;first;</td></tr>
<tr><td class="num" id="LN124">124</td><td class="line"> </td></tr>
<tr><td class="num" id="LN125">125</td><td class="line"> printf(<span class='string_literal'>"delete Node(%p) at beginning: '%d' '%s' '%p' \n"</span>, oldFirst,oldFirst-&gt;key, oldFirst-&gt;string, oldFirst-&gt;next);</td></tr>
<tr><td class="num" id="LN126">126</td><td class="line"> </td></tr>
<tr><td class="num" id="LN127">127</td><td class="line"> <span class="mrange">free(list-&gt;first)</span>; <span class='comment'>//free it</span></td></tr>
<tr><td class="num"></td><td class="line"><div id="Path4" class="msg msgEvent" style="margin-left:5ex"><table class="msgT"><tr><td valign="top"><div class="PathIndex PathIndexEvent">4</div></td><td><div class="PathNav"><a href="#Path3" title="Previous event (3)">&#x2190;</a></div></td></td><td>Memory is released</td><td><div class="PathNav"><a href="#EndPath" title="Next event (5)">&#x2192;</a></div></td></tr></table></div></td></tr>
<tr><td class="num" id="LN128">128</td><td class="line"> list-&gt;first = <span class="mrange">oldFirst-&gt;next</span>;</td></tr>
<tr><td class="num"></td><td class="line"><div id="EndPath" class="msg msgEvent" style="margin-left:19ex"><table class="msgT"><tr><td valign="top"><div class="PathIndex PathIndexEvent">5</div></td><td><div class="PathNav"><a href="#Path4" title="Previous event (4)">&#x2190;</a></div></td></td><td>Use of memory after it is freed</td></tr></table></div></td></tr>
<tr><td class="num" id="LN129">129</td><td class="line"> oldFirst = <span class='macro'>NULL<span class='expansion'>((void *)0)</span></span>;</td></tr>
<tr><td class="num" id="LN130">130</td><td class="line"> </td></tr>
<tr><td class="num" id="LN131">131</td><td class="line"> <span class='keyword'>return</span> <span class='macro'>OK<span class='expansion'>0</span></span>;</td></tr>
<tr><td class="num" id="LN132">132</td><td class="line">}</td></tr>
<tr><td class="num" id="LN133">133</td><td class="line"> </td></tr>
<tr><td class="num" id="LN134">134</td><td class="line"><span class='comment'>/**</span></td></tr>
<tr><td class="num" id="LN135">135</td><td class="line"> <span class='comment'>* Removes the last Node from the list.</span></td></tr>
<tr><td class="num" id="LN136">136</td><td class="line"> <span class='comment'>*</span></td></tr>
<tr><td class="num" id="LN137">137</td><td class="line"> <span class='comment'>* @param list LinkedList * .. ptr to the List</span></td></tr>
<tr><td class="num" id="LN138">138</td><td class="line"> <span class='comment'>*</span></td></tr>
<tr><td class="num" id="LN139">139</td><td class="line"> <span class='comment'>* @return OK | ERROR</span></td></tr>
<tr><td class="num" id="LN140">140</td><td class="line"> <span class='comment'>*/</span></td></tr>
<tr><td class="num" id="LN141">141</td><td class="line"><span class='keyword'>int</span> remove_end(LinkedList *list) {</td></tr>
<tr><td class="num" id="LN142">142</td><td class="line"> printf(<span class='string_literal'>"----------------------\n"</span>);</td></tr>
<tr><td class="num" id="LN143">143</td><td class="line"> </td></tr>
<tr><td class="num" id="LN144">144</td><td class="line"> <span class='comment'>/* special case #1 */</span></td></tr>
<tr><td class="num" id="LN145">145</td><td class="line"> <span class='keyword'>if</span> (list-&gt;size &lt;= 0)</td></tr>
<tr><td class="num" id="LN146">146</td><td class="line"> <span class='keyword'>return</span> <span class='macro'>ERROR<span class='expansion'>-1</span></span>;</td></tr>
<tr><td class="num" id="LN147">147</td><td class="line"> </td></tr>
<tr><td class="num" id="LN148">148</td><td class="line"> <span class='comment'>/* special case #2 */</span></td></tr>
<tr><td class="num" id="LN149">149</td><td class="line"> <span class='keyword'>if</span> (list-&gt;size == 1) {</td></tr>
<tr><td class="num" id="LN150">150</td><td class="line"> free(list-&gt;first);</td></tr>
<tr><td class="num" id="LN151">151</td><td class="line"> list-&gt;first = <span class='macro'>NULL<span class='expansion'>((void *)0)</span></span>;</td></tr>
<tr><td class="num" id="LN152">152</td><td class="line"> list-&gt;last = <span class='macro'>NULL<span class='expansion'>((void *)0)</span></span>;</td></tr>
<tr><td class="num" id="LN153">153</td><td class="line"> <span class='keyword'>return</span> <span class='macro'>OK<span class='expansion'>0</span></span>;</td></tr>
<tr><td class="num" id="LN154">154</td><td class="line"> }</td></tr>
<tr><td class="num" id="LN155">155</td><td class="line"> </td></tr>
<tr><td class="num" id="LN156">156</td><td class="line"> printf(<span class='string_literal'>"delete Node(%p) at end: '%d' '%s' '%p' \n"</span>, list-&gt;last,list-&gt;last-&gt;key, list-&gt;last-&gt;string, list-&gt;last-&gt;next);</td></tr>
<tr><td class="num" id="LN157">157</td><td class="line"> </td></tr>
<tr><td class="num" id="LN158">158</td><td class="line"> list-&gt;size--; <span class='comment'>// decrement list size</span></td></tr>
<tr><td class="num" id="LN159">159</td><td class="line"> Node * startNode = list-&gt;first;</td></tr>
<tr><td class="num" id="LN160">160</td><td class="line"> </td></tr>
<tr><td class="num" id="LN161">161</td><td class="line"> <span class='comment'>/* find the new last node (the one before the old last one); list-&gt;size &gt;= 2 at this point!*/</span></td></tr>
<tr><td class="num" id="LN162">162</td><td class="line"> Node * newLast = startNode;</td></tr>
<tr><td class="num" id="LN163">163</td><td class="line"> <span class='keyword'>while</span> (newLast-&gt;next-&gt;next != <span class='macro'>NULL<span class='expansion'>((void *)0)</span></span>) {</td></tr>
<tr><td class="num" id="LN164">164</td><td class="line"> newLast = newLast-&gt;next;</td></tr>
<tr><td class="num" id="LN165">165</td><td class="line"> }</td></tr>
<tr><td class="num" id="LN166">166</td><td class="line"> </td></tr>
<tr><td class="num" id="LN167">167</td><td class="line"> free(newLast-&gt;next); <span class='comment'>//free it</span></td></tr>
<tr><td class="num" id="LN168">168</td><td class="line"> newLast-&gt;next = <span class='macro'>NULL<span class='expansion'>((void *)0)</span></span>; <span class='comment'>//set to NULL to denote new end of list</span></td></tr>
<tr><td class="num" id="LN169">169</td><td class="line"> list-&gt;last = newLast; <span class='comment'>// set the new list-&gt;last</span></td></tr>
<tr><td class="num" id="LN170">170</td><td class="line"> </td></tr>
<tr><td class="num" id="LN171">171</td><td class="line"> <span class='keyword'>return</span> <span class='macro'>OK<span class='expansion'>0</span></span>;</td></tr>
<tr><td class="num" id="LN172">172</td><td class="line">}</td></tr>
<tr><td class="num" id="LN173">173</td><td class="line"> </td></tr>
<tr><td class="num" id="LN174">174</td><td class="line"><span class='comment'>/**</span></td></tr>
<tr><td class="num" id="LN175">175</td><td class="line"> <span class='comment'>* Given a List prints all key/string pairs contained in the list to</span></td></tr>
<tr><td class="num" id="LN176">176</td><td class="line"> <span class='comment'>* the screen</span></td></tr>
<tr><td class="num" id="LN177">177</td><td class="line"> <span class='comment'>*</span></td></tr>
<tr><td class="num" id="LN178">178</td><td class="line"> <span class='comment'>* @param list LinkedList * .. ptr to the List</span></td></tr>
<tr><td class="num" id="LN179">179</td><td class="line"> <span class='comment'>*</span></td></tr>
<tr><td class="num" id="LN180">180</td><td class="line"> <span class='comment'>* @return OK | ERROR</span></td></tr>
<tr><td class="num" id="LN181">181</td><td class="line"> <span class='comment'>*/</span></td></tr>
<tr><td class="num" id="LN182">182</td><td class="line"><span class='keyword'>int</span> print_list(LinkedList *list) {</td></tr>
<tr><td class="num" id="LN183">183</td><td class="line"> </td></tr>
<tr><td class="num" id="LN184">184</td><td class="line"> printf(<span class='string_literal'>"----------------------\n"</span>);</td></tr>
<tr><td class="num" id="LN185">185</td><td class="line"> </td></tr>
<tr><td class="num" id="LN186">186</td><td class="line"> <span class='keyword'>if</span> (list-&gt;size &lt;= 0)</td></tr>
<tr><td class="num" id="LN187">187</td><td class="line"> <span class='keyword'>return</span> <span class='macro'>ERROR<span class='expansion'>-1</span></span>;</td></tr>
<tr><td class="num" id="LN188">188</td><td class="line"> </td></tr>
<tr><td class="num" id="LN189">189</td><td class="line"> printf(<span class='string_literal'>"List.size = %d \n"</span>, list-&gt;size);</td></tr>
<tr><td class="num" id="LN190">190</td><td class="line"> </td></tr>
<tr><td class="num" id="LN191">191</td><td class="line"> Node *startN = list-&gt;first; <span class='comment'>//get first</span></td></tr>
<tr><td class="num" id="LN192">192</td><td class="line"> </td></tr>
<tr><td class="num" id="LN193">193</td><td class="line"> <span class='comment'>/* iterate through list and print contents */</span></td></tr>
<tr><td class="num" id="LN194">194</td><td class="line"> <span class='keyword'>do</span> {</td></tr>
<tr><td class="num" id="LN195">195</td><td class="line"> printf(<span class='string_literal'>"Node#%d.string = '%s', .next = '%p' \n"</span>, startN-&gt;key,startN-&gt;string, startN-&gt;next);</td></tr>
<tr><td class="num" id="LN196">196</td><td class="line"> startN = startN-&gt;next;</td></tr>
<tr><td class="num" id="LN197">197</td><td class="line"> } <span class='keyword'>while</span> (startN != <span class='macro'>NULL<span class='expansion'>((void *)0)</span></span>);</td></tr>
<tr><td class="num" id="LN198">198</td><td class="line"> </td></tr>
<tr><td class="num" id="LN199">199</td><td class="line"> <span class='keyword'>return</span> <span class='macro'>OK<span class='expansion'>0</span></span>;</td></tr>
<tr><td class="num" id="LN200">200</td><td class="line">}</td></tr>
<tr><td class="num" id="LN201">201</td><td class="line"> </td></tr>
<tr><td class="num" id="LN202">202</td><td class="line"><span class='comment'>/**</span></td></tr>
<tr><td class="num" id="LN203">203</td><td class="line"> <span class='comment'>* Given a List, frees all memory associated with this list.</span></td></tr>
<tr><td class="num" id="LN204">204</td><td class="line"> <span class='comment'>*</span></td></tr>
<tr><td class="num" id="LN205">205</td><td class="line"> <span class='comment'>* @param list LinkedList * ..ptr to the list</span></td></tr>
<tr><td class="num" id="LN206">206</td><td class="line"> <span class='comment'>*/</span></td></tr>
<tr><td class="num" id="LN207">207</td><td class="line"><span class='keyword'>void</span> free_list(LinkedList *list) {</td></tr>
<tr><td class="num" id="LN208">208</td><td class="line"> printf(<span class='string_literal'>"----------------------\n"</span>);</td></tr>
<tr><td class="num" id="LN209">209</td><td class="line"> printf(<span class='string_literal'>"freeing list...\n"</span>);</td></tr>
<tr><td class="num" id="LN210">210</td><td class="line"> </td></tr>
<tr><td class="num" id="LN211">211</td><td class="line"> <span class='keyword'>if</span> (list != <span class='macro'>NULL<span class='expansion'>((void *)0)</span></span> &amp;&amp; list-&gt;size &gt; 0) {</td></tr>
<tr><td class="num" id="LN212">212</td><td class="line"> Node * startN = list-&gt;first;</td></tr>
<tr><td class="num" id="LN213">213</td><td class="line"> Node * temp = list-&gt;first;</td></tr>
<tr><td class="num" id="LN214">214</td><td class="line"> </td></tr>
<tr><td class="num" id="LN215">215</td><td class="line"> <span class='keyword'>do</span> {</td></tr>
<tr><td class="num" id="LN216">216</td><td class="line"> free(temp);</td></tr>
<tr><td class="num" id="LN217">217</td><td class="line"> startN = startN-&gt;next;</td></tr>
<tr><td class="num" id="LN218">218</td><td class="line"> temp = startN;</td></tr>
<tr><td class="num" id="LN219">219</td><td class="line"> } <span class='keyword'>while</span> (startN != <span class='macro'>NULL<span class='expansion'>((void *)0)</span></span>);</td></tr>
<tr><td class="num" id="LN220">220</td><td class="line"> </td></tr>
<tr><td class="num" id="LN221">221</td><td class="line"> free(list);</td></tr>
<tr><td class="num" id="LN222">222</td><td class="line"> }</td></tr>
<tr><td class="num" id="LN223">223</td><td class="line">}</td></tr>
<tr><td class="num" id="LN224">224</td><td class="line"> </td></tr>
<tr><td class="num" id="LN225">225</td><td class="line"><span class='comment'>/**</span></td></tr>
<tr><td class="num" id="LN226">226</td><td class="line"> <span class='comment'>* Given a List and a key, iterates through the whole List and returns</span></td></tr>
<tr><td class="num" id="LN227">227</td><td class="line"> <span class='comment'>* the string of the first node which contains the key</span></td></tr>
<tr><td class="num" id="LN228">228</td><td class="line"> <span class='comment'>*</span></td></tr>
<tr><td class="num" id="LN229">229</td><td class="line"> <span class='comment'>* @param list LinkedList * ..ptr to the list</span></td></tr>
<tr><td class="num" id="LN230">230</td><td class="line"> <span class='comment'>* @param key int .. the key of the Node to get the String from</span></td></tr>
<tr><td class="num" id="LN231">231</td><td class="line"> <span class='comment'>*</span></td></tr>
<tr><td class="num" id="LN232">232</td><td class="line"> <span class='comment'>* @return OK | ERROR</span></td></tr>
<tr><td class="num" id="LN233">233</td><td class="line"> <span class='comment'>*/</span></td></tr>
<tr><td class="num" id="LN234">234</td><td class="line"><span class='keyword'>char</span> * get_string(LinkedList *list, <span class='keyword'>int</span> key) {</td></tr>
<tr><td class="num" id="LN235">235</td><td class="line"> printf(<span class='string_literal'>"----------------------\n"</span>);</td></tr>
<tr><td class="num" id="LN236">236</td><td class="line"> </td></tr>
<tr><td class="num" id="LN237">237</td><td class="line"> Node *startN = list-&gt;first; <span class='comment'>//get first</span></td></tr>
<tr><td class="num" id="LN238">238</td><td class="line"> </td></tr>
<tr><td class="num" id="LN239">239</td><td class="line"> <span class='comment'>/* if only one node.. */</span></td></tr>
<tr><td class="num" id="LN240">240</td><td class="line"> <span class='keyword'>if</span>(list-&gt;size == 1)</td></tr>
<tr><td class="num" id="LN241">241</td><td class="line"> <span class='keyword'>return</span> startN-&gt;string;</td></tr>
<tr><td class="num" id="LN242">242</td><td class="line"> </td></tr>
<tr><td class="num" id="LN243">243</td><td class="line"> <span class='comment'>/* iterate through list and find Node where node-&gt;key == key */</span></td></tr>
<tr><td class="num" id="LN244">244</td><td class="line"> <span class='keyword'>while</span> (startN-&gt;next != <span class='macro'>NULL<span class='expansion'>((void *)0)</span></span>) {</td></tr>
<tr><td class="num" id="LN245">245</td><td class="line"> <span class='keyword'>if</span> (startN-&gt;key == key)</td></tr>
<tr><td class="num" id="LN246">246</td><td class="line"> <span class='keyword'>return</span> startN-&gt;string;</td></tr>
<tr><td class="num" id="LN247">247</td><td class="line"> <span class='keyword'>else</span></td></tr>
<tr><td class="num" id="LN248">248</td><td class="line"> startN = startN-&gt;next;</td></tr>
<tr><td class="num" id="LN249">249</td><td class="line"> }</td></tr>
<tr><td class="num" id="LN250">250</td><td class="line"> </td></tr>
<tr><td class="num" id="LN251">251</td><td class="line"> <span class='keyword'>return</span> <span class='macro'>NULL<span class='expansion'>((void *)0)</span></span>;</td></tr>
<tr><td class="num" id="LN252">252</td><td class="line">}</td></tr>
<tr><td class="num" id="LN253">253</td><td class="line"> </td></tr>
<tr><td class="num" id="LN254">254</td><td class="line"><span class='comment'>/*************** MAIN **************/</span></td></tr>
<tr><td class="num" id="LN255">255</td><td class="line"><span class='keyword'>int</span> main(<span class='keyword'>void</span>) {</td></tr>
<tr><td class="num" id="LN256">256</td><td class="line"> </td></tr>
<tr><td class="num" id="LN257">257</td><td class="line"> LinkedList *list = init_list();</td></tr>
<tr><td class="num" id="LN258">258</td><td class="line"> </td></tr>
<tr><td class="num" id="LN259">259</td><td class="line"> insert_beginning(list, 1, <span class='string_literal'>"im the first"</span>);</td></tr>
<tr><td class="num" id="LN260">260</td><td class="line"> insert_end(list, 2, <span class='string_literal'>"im the second"</span>);</td></tr>
<tr><td class="num" id="LN261">261</td><td class="line"> insert_end(list, 3, <span class='string_literal'>"im the third"</span>);</td></tr>
<tr><td class="num" id="LN262">262</td><td class="line"> insert_end(list, 4, <span class='string_literal'>"forth here"</span>);</td></tr>
<tr><td class="num" id="LN263">263</td><td class="line"> </td></tr>
<tr><td class="num" id="LN264">264</td><td class="line"> print_list(list);</td></tr>
<tr><td class="num" id="LN265">265</td><td class="line"> remove_end(list);</td></tr>
<tr><td class="num" id="LN266">266</td><td class="line"> print_list(list);</td></tr>
<tr><td class="num" id="LN267">267</td><td class="line"> <span class="mrange">remove_beginning(list)</span>;</td></tr>
<tr><td class="num"></td><td class="line"><div id="Path1" class="msg msgEvent" style="margin-left:5ex"><table class="msgT"><tr><td valign="top"><div class="PathIndex PathIndexEvent">1</div></td><td>Calling 'remove_beginning'</td><td><div class="PathNav"><a href="#Path2" title="Next event (2)">&#x2192;</a></div></td></tr></table></div></td></tr>
<tr><td class="num" id="LN268">268</td><td class="line"> print_list(list);</td></tr>
<tr><td class="num" id="LN269">269</td><td class="line"> remove_end(list);</td></tr>
<tr><td class="num" id="LN270">270</td><td class="line"> print_list(list);</td></tr>
<tr><td class="num" id="LN271">271</td><td class="line"> printf(<span class='string_literal'>"string at node with key %d = '%s' \n"</span>,2,get_string(list, 2));</td></tr>
<tr><td class="num" id="LN272">272</td><td class="line"> free_list(list);</td></tr>
<tr><td class="num" id="LN273">273</td><td class="line"> </td></tr>
<tr><td class="num" id="LN274">274</td><td class="line"> <span class='keyword'>return</span> <span class='macro'>OK<span class='expansion'>0</span></span>;</td></tr>
<tr><td class="num" id="LN275">275</td><td class="line">}</td></tr></table></body></html>
<!doctype html>
<html>
<head>
<title>list.c</title>
<style type="text/css">
body { color:#000000; background-color:#ffffff }
body { font-family:Helvetica, sans-serif; font-size:10pt }
h1 { font-size:14pt }
.code { border-collapse:collapse; width:100%; }
.code { font-family: "Monospace", monospace; font-size:10pt }
.code { line-height: 1.2em }
.comment { color: green; font-style: oblique }
.keyword { color: blue }
.string_literal { color: red }
.directive { color: darkmagenta }
.expansion { display: none; }
.macro:hover .expansion { display: block; border: 2px solid #FF0000; padding: 2px; background-color:#FFF0F0; font-weight: normal; -webkit-border-radius:5px; -webkit-box-shadow:1px 1px 7px #000; border-radius:5px; box-shadow:1px 1px 7px #000; position: absolute; top: -1em; left:10em; z-index: 1 }
.macro { color: darkmagenta; background-color:LemonChiffon; position: relative }
.num { width:2.5em; padding-right:2ex; background-color:#eeeeee }
.num { text-align:right; font-size:8pt }
.num { color:#444444 }
.line { padding-left: 1ex; border-left: 3px solid #ccc }
.line { white-space: pre }
.msg { -webkit-box-shadow:1px 1px 7px #000 }
.msg { box-shadow:1px 1px 7px #000 }
.msg { -webkit-border-radius:5px }
.msg { border-radius:5px }
.msg { font-family:Helvetica, sans-serif; font-size:8pt }
.msg { float:left }
.msg { padding:0.25em 1ex 0.25em 1ex }
.msg { margin-top:10px; margin-bottom:10px }
.msg { font-weight:bold }
.msg { max-width:60em; word-wrap: break-word; white-space: pre-wrap }
.msgT { padding:0x; spacing:0x }
.msgEvent { background-color:#fff8b4; color:#000000 }
.msgControl { background-color:#bbbbbb; color:#000000 }
.msgNote { background-color:#ddeeff; color:#000000 }
.mrange { background-color:#dfddf3 }
.mrange { border-bottom:1px solid #6F9DBE }
.PathIndex { font-weight: bold; padding:0px 5px; margin-right:5px; }
.PathIndex { -webkit-border-radius:8px }
.PathIndex { border-radius:8px }
.PathIndexEvent { background-color:#bfba87 }
.PathIndexControl { background-color:#8c8c8c }
.PathNav a { text-decoration:none; font-size: larger }
.CodeInsertionHint { font-weight: bold; background-color: #10dd10 }
.CodeRemovalHint { background-color:#de1010 }
.CodeRemovalHint { border-bottom:1px solid #6F9DBE }
table.simpletable {
padding: 5px;
font-size:12pt;
margin:20px;
border-collapse: collapse; border-spacing: 0px;
}
td.rowname {
text-align: right;
vertical-align: top;
font-weight: bold;
color:#444444;
padding-right:2ex;
}
</style>
</head>
<body>
<!-- BUGDESC Use of memory after it is freed -->
<!-- BUGTYPE Use-after-free -->
<!-- BUGCATEGORY Memory Error -->
<!-- BUGFILE /Users/joerg/Documents/list.c -->
<!-- FILENAME list.c -->
<!-- FUNCTIONNAME free_list -->
<!-- ISSUEHASHCONTENTOFLINEINCONTEXT 2027cb9177b79370ca3b6d8c0be404b2 -->
<!-- BUGLINE 217 -->
<!-- BUGCOLUMN 22 -->
<!-- BUGPATHLENGTH 5 -->
<!-- BUGMETAEND -->
<!-- REPORTHEADER -->
<h3>Bug Summary</h3>
<table class="simpletable">
<tr><td class="rowname">File:</td><td>/Users/joerg/Documents/list.c</td></tr>
<tr><td class="rowname">Warning:</td><td><a href="#EndPath">line 217, column 22</a><br />Use of memory after it is freed</td></tr>
</table>
<!-- REPORTSUMMARYEXTRA -->
<h3>Annotated Source Code</h3>
<table class="code">
<tr><td class="num" id="LN1">1</td><td class="line"><span class='directive'>#include &lt;stdio.h&gt;</span></td></tr>
<tr><td class="num" id="LN2">2</td><td class="line"><span class='directive'>#include &lt;stdlib.h&gt;</span></td></tr>
<tr><td class="num" id="LN3">3</td><td class="line"><span class='directive'>#include &lt;string.h&gt;</span></td></tr>
<tr><td class="num" id="LN4">4</td><td class="line"> </td></tr>
<tr><td class="num" id="LN5">5</td><td class="line"><span class='comment'>/********** GLOBALS *******************************/</span></td></tr>
<tr><td class="num" id="LN6">6</td><td class="line"><span class='directive'>#define <span class='macro'>OK<span class='expansion'>0</span></span> 0</span></td></tr>
<tr><td class="num" id="LN7">7</td><td class="line"><span class='directive'>#define <span class='macro'>ERROR<span class='expansion'>-1</span></span> -1</span></td></tr>
<tr><td class="num" id="LN8">8</td><td class="line"> </td></tr>
<tr><td class="num" id="LN9">9</td><td class="line"><span class='comment'>/********** STRUCT AND TYPES DEFINTIONS ***********/</span></td></tr>
<tr><td class="num" id="LN10">10</td><td class="line"><span class='comment'>/* a node with key, data and reference to next node*/</span></td></tr>
<tr><td class="num" id="LN11">11</td><td class="line"><span class='keyword'>typedef</span> <span class='keyword'>struct</span> Node {</td></tr>
<tr><td class="num" id="LN12">12</td><td class="line"> <span class='keyword'>int</span> key;</td></tr>
<tr><td class="num" id="LN13">13</td><td class="line"> <span class='keyword'>char</span> string[1024];</td></tr>
<tr><td class="num" id="LN14">14</td><td class="line"> <span class='keyword'>struct</span> Node *next; <span class='comment'>// pointer to next node</span></td></tr>
<tr><td class="num" id="LN15">15</td><td class="line">} Node;</td></tr>
<tr><td class="num" id="LN16">16</td><td class="line"> </td></tr>
<tr><td class="num" id="LN17">17</td><td class="line"><span class='comment'>/* the actual linked list: ref to first and last Node, size attribute */</span></td></tr>
<tr><td class="num" id="LN18">18</td><td class="line"><span class='keyword'>typedef</span> <span class='keyword'>struct</span> LinkedList {</td></tr>
<tr><td class="num" id="LN19">19</td><td class="line"> <span class='keyword'>struct</span> Node *first;</td></tr>
<tr><td class="num" id="LN20">20</td><td class="line"> <span class='keyword'>struct</span> Node *last;</td></tr>
<tr><td class="num" id="LN21">21</td><td class="line"> <span class='keyword'>int</span> size;</td></tr>
<tr><td class="num" id="LN22">22</td><td class="line">} LinkedList;</td></tr>
<tr><td class="num" id="LN23">23</td><td class="line"> </td></tr>
<tr><td class="num" id="LN24">24</td><td class="line"><span class='comment'>/********** FUNCTION HEADERS **********************/</span></td></tr>
<tr><td class="num" id="LN25">25</td><td class="line">LinkedList* init_list();</td></tr>
<tr><td class="num" id="LN26">26</td><td class="line"><span class='keyword'>void</span> insert_end(LinkedList *list, <span class='keyword'>int</span> key, <span class='keyword'>char</span> string[]);</td></tr>
<tr><td class="num" id="LN27">27</td><td class="line"><span class='keyword'>void</span> insert_beginning(LinkedList *list, <span class='keyword'>int</span> key, <span class='keyword'>char</span> string[]);</td></tr>
<tr><td class="num" id="LN28">28</td><td class="line"><span class='keyword'>int</span> remove_end(LinkedList *list);</td></tr>
<tr><td class="num" id="LN29">29</td><td class="line"><span class='keyword'>int</span> remove_beginning(LinkedList *list);</td></tr>
<tr><td class="num" id="LN30">30</td><td class="line"><span class='keyword'>int</span> print_list(LinkedList *list);</td></tr>
<tr><td class="num" id="LN31">31</td><td class="line"><span class='keyword'>void</span> free_list(LinkedList *list);</td></tr>
<tr><td class="num" id="LN32">32</td><td class="line"><span class='keyword'>char</span> * get_string(LinkedList *list, <span class='keyword'>int</span> key);</td></tr>
<tr><td class="num" id="LN33">33</td><td class="line"> </td></tr>
<tr><td class="num" id="LN34">34</td><td class="line"><span class='comment'>/*********** FUNCTION DEFINITIONS ***************/</span></td></tr>
<tr><td class="num" id="LN35">35</td><td class="line"> </td></tr>
<tr><td class="num" id="LN36">36</td><td class="line"><span class='comment'>/**</span></td></tr>
<tr><td class="num" id="LN37">37</td><td class="line"> <span class='comment'>* init_list Returns an appropriately (for an empty list) initialized struct List</span></td></tr>
<tr><td class="num" id="LN38">38</td><td class="line"> <span class='comment'>*</span></td></tr>
<tr><td class="num" id="LN39">39</td><td class="line"> <span class='comment'>* @return LinkedList * ..ptr to the newly initialized list</span></td></tr>
<tr><td class="num" id="LN40">40</td><td class="line"> <span class='comment'>*/</span></td></tr>
<tr><td class="num" id="LN41">41</td><td class="line">LinkedList * init_list() {</td></tr>
<tr><td class="num" id="LN42">42</td><td class="line"> printf(<span class='string_literal'>"initializing list...\n"</span>);</td></tr>
<tr><td class="num" id="LN43">43</td><td class="line"> </td></tr>
<tr><td class="num" id="LN44">44</td><td class="line"> LinkedList *list = (LinkedList*) malloc(<span class='keyword'>sizeof</span>(LinkedList));</td></tr>
<tr><td class="num" id="LN45">45</td><td class="line"> </td></tr>
<tr><td class="num" id="LN46">46</td><td class="line"> list-&gt;first = <span class='macro'>NULL<span class='expansion'>((void *)0)</span></span>;</td></tr>
<tr><td class="num" id="LN47">47</td><td class="line"> list-&gt;last = <span class='macro'>NULL<span class='expansion'>((void *)0)</span></span>;</td></tr>
<tr><td class="num" id="LN48">48</td><td class="line"> list-&gt;size = 0;</td></tr>
<tr><td class="num" id="LN49">49</td><td class="line"> </td></tr>
<tr><td class="num" id="LN50">50</td><td class="line"> <span class='keyword'>return</span> list;</td></tr>
<tr><td class="num" id="LN51">51</td><td class="line">}</td></tr>
<tr><td class="num" id="LN52">52</td><td class="line"> </td></tr>
<tr><td class="num" id="LN53">53</td><td class="line"><span class='comment'>/**</span></td></tr>
<tr><td class="num" id="LN54">54</td><td class="line"> <span class='comment'>* Given a List, a key and a string adds a Node containing this</span></td></tr>
<tr><td class="num" id="LN55">55</td><td class="line"> <span class='comment'>* information at the end of the list</span></td></tr>
<tr><td class="num" id="LN56">56</td><td class="line"> <span class='comment'>*</span></td></tr>
<tr><td class="num" id="LN57">57</td><td class="line"> <span class='comment'>* @param list LinkedList * ..ptr to LinkedList</span></td></tr>
<tr><td class="num" id="LN58">58</td><td class="line"> <span class='comment'>* @param key int .. key of the Node to be inserted</span></td></tr>
<tr><td class="num" id="LN59">59</td><td class="line"> <span class='comment'>* @param string char[] .. the string of the Node to be inserted</span></td></tr>
<tr><td class="num" id="LN60">60</td><td class="line"> <span class='comment'>*/</span></td></tr>
<tr><td class="num" id="LN61">61</td><td class="line"><span class='keyword'>void</span> insert_end(LinkedList *list, <span class='keyword'>int</span> key, <span class='keyword'>char</span> string[]) {</td></tr>
<tr><td class="num" id="LN62">62</td><td class="line"> printf(<span class='string_literal'>"----------------------\n"</span>);</td></tr>
<tr><td class="num" id="LN63">63</td><td class="line"> </td></tr>
<tr><td class="num" id="LN64">64</td><td class="line"> list-&gt;size++; <span class='comment'>// increment size of list</span></td></tr>
<tr><td class="num" id="LN65">65</td><td class="line"> </td></tr>
<tr><td class="num" id="LN66">66</td><td class="line"> <span class='comment'>// intialize the new Node</span></td></tr>
<tr><td class="num" id="LN67">67</td><td class="line"> Node* newN = (Node*) malloc(<span class='keyword'>sizeof</span>(Node));</td></tr>
<tr><td class="num" id="LN68">68</td><td class="line"> newN-&gt;key = key;</td></tr>
<tr><td class="num" id="LN69">69</td><td class="line"> <span class='macro'>strcpy(newN-&gt;string, string)<span class='expansion'>__builtin___strcpy_chk (newN-&gt;string, string, __builtin_object_size<br> (newN-&gt;string, 2 &gt; 1 ? 1 : 0))</span></span>;</td></tr>
<tr><td class="num" id="LN70">70</td><td class="line"> newN-&gt;next = <span class='macro'>NULL<span class='expansion'>((void *)0)</span></span>;</td></tr>
<tr><td class="num" id="LN71">71</td><td class="line"> </td></tr>
<tr><td class="num" id="LN72">72</td><td class="line"> Node* oldLast = list-&gt;last; <span class='comment'>// get the old last</span></td></tr>
<tr><td class="num" id="LN73">73</td><td class="line"> oldLast-&gt;next = newN; <span class='comment'>// make new Node the next Node for oldlast</span></td></tr>
<tr><td class="num" id="LN74">74</td><td class="line"> list-&gt;last = newN; <span class='comment'>// set the new last in the list</span></td></tr>
<tr><td class="num" id="LN75">75</td><td class="line"> </td></tr>
<tr><td class="num" id="LN76">76</td><td class="line"> printf(<span class='string_literal'>"new Node(%p) at end: %d '%s' %p \n"</span>, newN, newN-&gt;key, newN-&gt;string,newN-&gt;next);</td></tr>
<tr><td class="num" id="LN77">77</td><td class="line">}</td></tr>
<tr><td class="num" id="LN78">78</td><td class="line"> </td></tr>
<tr><td class="num" id="LN79">79</td><td class="line"><span class='comment'>/**</span></td></tr>
<tr><td class="num" id="LN80">80</td><td class="line"> <span class='comment'>* Given a List, a key and a string adds a Node, containing</span></td></tr>
<tr><td class="num" id="LN81">81</td><td class="line"> <span class='comment'>* this information at the beginning of the list</span></td></tr>
<tr><td class="num" id="LN82">82</td><td class="line"> <span class='comment'>*</span></td></tr>
<tr><td class="num" id="LN83">83</td><td class="line"> <span class='comment'>* @param list LinkedList * ..ptr to LinkedList</span></td></tr>
<tr><td class="num" id="LN84">84</td><td class="line"> <span class='comment'>* @param key int .. key of the Node to be inserted</span></td></tr>
<tr><td class="num" id="LN85">85</td><td class="line"> <span class='comment'>* @param string char[] .. the string of the Node to be inserted</span></td></tr>
<tr><td class="num" id="LN86">86</td><td class="line"> <span class='comment'>*/</span></td></tr>
<tr><td class="num" id="LN87">87</td><td class="line"><span class='keyword'>void</span> insert_beginning(LinkedList *list, <span class='keyword'>int</span> key, <span class='keyword'>char</span> string[]) {</td></tr>
<tr><td class="num" id="LN88">88</td><td class="line"> printf(<span class='string_literal'>"----------------------\n"</span>);</td></tr>
<tr><td class="num" id="LN89">89</td><td class="line"> </td></tr>
<tr><td class="num" id="LN90">90</td><td class="line"> list-&gt;size++; <span class='comment'>// increment size of list</span></td></tr>
<tr><td class="num" id="LN91">91</td><td class="line"> Node* oldFirst = list-&gt;first; <span class='comment'>//get the old first node</span></td></tr>
<tr><td class="num" id="LN92">92</td><td class="line"> </td></tr>
<tr><td class="num" id="LN93">93</td><td class="line"> <span class='comment'>/* intialize the new Node */</span></td></tr>
<tr><td class="num" id="LN94">94</td><td class="line"> Node* newN = (Node*) malloc(<span class='keyword'>sizeof</span>(Node));</td></tr>
<tr><td class="num" id="LN95">95</td><td class="line"> newN-&gt;key = key;</td></tr>
<tr><td class="num" id="LN96">96</td><td class="line"> <span class='macro'>strcpy(newN-&gt;string, string)<span class='expansion'>__builtin___strcpy_chk (newN-&gt;string, string, __builtin_object_size<br> (newN-&gt;string, 2 &gt; 1 ? 1 : 0))</span></span>;</td></tr>
<tr><td class="num" id="LN97">97</td><td class="line"> newN-&gt;next = oldFirst;</td></tr>
<tr><td class="num" id="LN98">98</td><td class="line"> </td></tr>
<tr><td class="num" id="LN99">99</td><td class="line"> list-&gt;first = newN; <span class='comment'>// set the new first</span></td></tr>
<tr><td class="num" id="LN100">100</td><td class="line"> </td></tr>
<tr><td class="num" id="LN101">101</td><td class="line"> <span class='comment'>/* special case: if list size == 1, then this new one is also the last one */</span></td></tr>
<tr><td class="num" id="LN102">102</td><td class="line"> <span class='keyword'>if</span> (list-&gt;size == 1)</td></tr>
<tr><td class="num" id="LN103">103</td><td class="line"> list-&gt;last = newN;</td></tr>
<tr><td class="num" id="LN104">104</td><td class="line"> </td></tr>
<tr><td class="num" id="LN105">105</td><td class="line"> printf(<span class='string_literal'>"new Node(%p) at beginning: %d '%s' %p \n"</span>, newN, newN-&gt;key,newN-&gt;string, newN-&gt;next);</td></tr>
<tr><td class="num" id="LN106">106</td><td class="line">}</td></tr>
<tr><td class="num" id="LN107">107</td><td class="line"> </td></tr>
<tr><td class="num" id="LN108">108</td><td class="line"><span class='comment'>/**</span></td></tr>
<tr><td class="num" id="LN109">109</td><td class="line"> <span class='comment'>* Removes the first Node from the list</span></td></tr>
<tr><td class="num" id="LN110">110</td><td class="line"> <span class='comment'>*</span></td></tr>
<tr><td class="num" id="LN111">111</td><td class="line"> <span class='comment'>* @param list LinkedList * .. ptr to the List</span></td></tr>
<tr><td class="num" id="LN112">112</td><td class="line"> <span class='comment'>*</span></td></tr>
<tr><td class="num" id="LN113">113</td><td class="line"> <span class='comment'>* @return OK | ERROR</span></td></tr>
<tr><td class="num" id="LN114">114</td><td class="line"> <span class='comment'>*/</span></td></tr>
<tr><td class="num" id="LN115">115</td><td class="line"><span class='keyword'>int</span> remove_beginning(LinkedList *list) {</td></tr>
<tr><td class="num" id="LN116">116</td><td class="line"> printf(<span class='string_literal'>"----------------------\n"</span>);</td></tr>
<tr><td class="num" id="LN117">117</td><td class="line"> </td></tr>
<tr><td class="num" id="LN118">118</td><td class="line"> <span class='keyword'>if</span> (list-&gt;size &lt;= 0)</td></tr>
<tr><td class="num" id="LN119">119</td><td class="line"> <span class='keyword'>return</span> <span class='macro'>ERROR<span class='expansion'>-1</span></span>;</td></tr>
<tr><td class="num" id="LN120">120</td><td class="line"> </td></tr>
<tr><td class="num" id="LN121">121</td><td class="line"> list-&gt;size--;</td></tr>
<tr><td class="num" id="LN122">122</td><td class="line"> </td></tr>
<tr><td class="num" id="LN123">123</td><td class="line"> Node * oldFirst = list-&gt;first;</td></tr>
<tr><td class="num" id="LN124">124</td><td class="line"> </td></tr>
<tr><td class="num" id="LN125">125</td><td class="line"> printf(<span class='string_literal'>"delete Node(%p) at beginning: '%d' '%s' '%p' \n"</span>, oldFirst,oldFirst-&gt;key, oldFirst-&gt;string, oldFirst-&gt;next);</td></tr>
<tr><td class="num" id="LN126">126</td><td class="line"> </td></tr>
<tr><td class="num" id="LN127">127</td><td class="line"> free(list-&gt;first); <span class='comment'>//free it</span></td></tr>
<tr><td class="num" id="LN128">128</td><td class="line"> list-&gt;first = oldFirst-&gt;next;</td></tr>
<tr><td class="num" id="LN129">129</td><td class="line"> oldFirst = <span class='macro'>NULL<span class='expansion'>((void *)0)</span></span>;</td></tr>
<tr><td class="num" id="LN130">130</td><td class="line"> </td></tr>
<tr><td class="num" id="LN131">131</td><td class="line"> <span class='keyword'>return</span> <span class='macro'>OK<span class='expansion'>0</span></span>;</td></tr>
<tr><td class="num" id="LN132">132</td><td class="line">}</td></tr>
<tr><td class="num" id="LN133">133</td><td class="line"> </td></tr>
<tr><td class="num" id="LN134">134</td><td class="line"><span class='comment'>/**</span></td></tr>
<tr><td class="num" id="LN135">135</td><td class="line"> <span class='comment'>* Removes the last Node from the list.</span></td></tr>
<tr><td class="num" id="LN136">136</td><td class="line"> <span class='comment'>*</span></td></tr>
<tr><td class="num" id="LN137">137</td><td class="line"> <span class='comment'>* @param list LinkedList * .. ptr to the List</span></td></tr>
<tr><td class="num" id="LN138">138</td><td class="line"> <span class='comment'>*</span></td></tr>
<tr><td class="num" id="LN139">139</td><td class="line"> <span class='comment'>* @return OK | ERROR</span></td></tr>
<tr><td class="num" id="LN140">140</td><td class="line"> <span class='comment'>*/</span></td></tr>
<tr><td class="num" id="LN141">141</td><td class="line"><span class='keyword'>int</span> remove_end(LinkedList *list) {</td></tr>
<tr><td class="num" id="LN142">142</td><td class="line"> printf(<span class='string_literal'>"----------------------\n"</span>);</td></tr>
<tr><td class="num" id="LN143">143</td><td class="line"> </td></tr>
<tr><td class="num" id="LN144">144</td><td class="line"> <span class='comment'>/* special case #1 */</span></td></tr>
<tr><td class="num" id="LN145">145</td><td class="line"> <span class='keyword'>if</span> (list-&gt;size &lt;= 0)</td></tr>
<tr><td class="num" id="LN146">146</td><td class="line"> <span class='keyword'>return</span> <span class='macro'>ERROR<span class='expansion'>-1</span></span>;</td></tr>
<tr><td class="num" id="LN147">147</td><td class="line"> </td></tr>
<tr><td class="num" id="LN148">148</td><td class="line"> <span class='comment'>/* special case #2 */</span></td></tr>
<tr><td class="num" id="LN149">149</td><td class="line"> <span class='keyword'>if</span> (list-&gt;size == 1) {</td></tr>
<tr><td class="num" id="LN150">150</td><td class="line"> free(list-&gt;first);</td></tr>
<tr><td class="num" id="LN151">151</td><td class="line"> list-&gt;first = <span class='macro'>NULL<span class='expansion'>((void *)0)</span></span>;</td></tr>
<tr><td class="num" id="LN152">152</td><td class="line"> list-&gt;last = <span class='macro'>NULL<span class='expansion'>((void *)0)</span></span>;</td></tr>
<tr><td class="num" id="LN153">153</td><td class="line"> <span class='keyword'>return</span> <span class='macro'>OK<span class='expansion'>0</span></span>;</td></tr>
<tr><td class="num" id="LN154">154</td><td class="line"> }</td></tr>
<tr><td class="num" id="LN155">155</td><td class="line"> </td></tr>
<tr><td class="num" id="LN156">156</td><td class="line"> printf(<span class='string_literal'>"delete Node(%p) at end: '%d' '%s' '%p' \n"</span>, list-&gt;last,list-&gt;last-&gt;key, list-&gt;last-&gt;string, list-&gt;last-&gt;next);</td></tr>
<tr><td class="num" id="LN157">157</td><td class="line"> </td></tr>
<tr><td class="num" id="LN158">158</td><td class="line"> list-&gt;size--; <span class='comment'>// decrement list size</span></td></tr>
<tr><td class="num" id="LN159">159</td><td class="line"> Node * startNode = list-&gt;first;</td></tr>
<tr><td class="num" id="LN160">160</td><td class="line"> </td></tr>
<tr><td class="num" id="LN161">161</td><td class="line"> <span class='comment'>/* find the new last node (the one before the old last one); list-&gt;size &gt;= 2 at this point!*/</span></td></tr>
<tr><td class="num" id="LN162">162</td><td class="line"> Node * newLast = startNode;</td></tr>
<tr><td class="num" id="LN163">163</td><td class="line"> <span class='keyword'>while</span> (newLast-&gt;next-&gt;next != <span class='macro'>NULL<span class='expansion'>((void *)0)</span></span>) {</td></tr>
<tr><td class="num" id="LN164">164</td><td class="line"> newLast = newLast-&gt;next;</td></tr>
<tr><td class="num" id="LN165">165</td><td class="line"> }</td></tr>
<tr><td class="num" id="LN166">166</td><td class="line"> </td></tr>
<tr><td class="num" id="LN167">167</td><td class="line"> free(newLast-&gt;next); <span class='comment'>//free it</span></td></tr>
<tr><td class="num" id="LN168">168</td><td class="line"> newLast-&gt;next = <span class='macro'>NULL<span class='expansion'>((void *)0)</span></span>; <span class='comment'>//set to NULL to denote new end of list</span></td></tr>
<tr><td class="num" id="LN169">169</td><td class="line"> list-&gt;last = newLast; <span class='comment'>// set the new list-&gt;last</span></td></tr>
<tr><td class="num" id="LN170">170</td><td class="line"> </td></tr>
<tr><td class="num" id="LN171">171</td><td class="line"> <span class='keyword'>return</span> <span class='macro'>OK<span class='expansion'>0</span></span>;</td></tr>
<tr><td class="num" id="LN172">172</td><td class="line">}</td></tr>
<tr><td class="num" id="LN173">173</td><td class="line"> </td></tr>
<tr><td class="num" id="LN174">174</td><td class="line"><span class='comment'>/**</span></td></tr>
<tr><td class="num" id="LN175">175</td><td class="line"> <span class='comment'>* Given a List prints all key/string pairs contained in the list to</span></td></tr>
<tr><td class="num" id="LN176">176</td><td class="line"> <span class='comment'>* the screen</span></td></tr>
<tr><td class="num" id="LN177">177</td><td class="line"> <span class='comment'>*</span></td></tr>
<tr><td class="num" id="LN178">178</td><td class="line"> <span class='comment'>* @param list LinkedList * .. ptr to the List</span></td></tr>
<tr><td class="num" id="LN179">179</td><td class="line"> <span class='comment'>*</span></td></tr>
<tr><td class="num" id="LN180">180</td><td class="line"> <span class='comment'>* @return OK | ERROR</span></td></tr>
<tr><td class="num" id="LN181">181</td><td class="line"> <span class='comment'>*/</span></td></tr>
<tr><td class="num" id="LN182">182</td><td class="line"><span class='keyword'>int</span> print_list(LinkedList *list) {</td></tr>
<tr><td class="num" id="LN183">183</td><td class="line"> </td></tr>
<tr><td class="num" id="LN184">184</td><td class="line"> printf(<span class='string_literal'>"----------------------\n"</span>);</td></tr>
<tr><td class="num" id="LN185">185</td><td class="line"> </td></tr>
<tr><td class="num" id="LN186">186</td><td class="line"> <span class='keyword'>if</span> (list-&gt;size &lt;= 0)</td></tr>
<tr><td class="num" id="LN187">187</td><td class="line"> <span class='keyword'>return</span> <span class='macro'>ERROR<span class='expansion'>-1</span></span>;</td></tr>
<tr><td class="num" id="LN188">188</td><td class="line"> </td></tr>
<tr><td class="num" id="LN189">189</td><td class="line"> printf(<span class='string_literal'>"List.size = %d \n"</span>, list-&gt;size);</td></tr>
<tr><td class="num" id="LN190">190</td><td class="line"> </td></tr>
<tr><td class="num" id="LN191">191</td><td class="line"> Node *startN = list-&gt;first; <span class='comment'>//get first</span></td></tr>
<tr><td class="num" id="LN192">192</td><td class="line"> </td></tr>
<tr><td class="num" id="LN193">193</td><td class="line"> <span class='comment'>/* iterate through list and print contents */</span></td></tr>
<tr><td class="num" id="LN194">194</td><td class="line"> <span class='keyword'>do</span> {</td></tr>
<tr><td class="num" id="LN195">195</td><td class="line"> printf(<span class='string_literal'>"Node#%d.string = '%s', .next = '%p' \n"</span>, startN-&gt;key,startN-&gt;string, startN-&gt;next);</td></tr>
<tr><td class="num" id="LN196">196</td><td class="line"> startN = startN-&gt;next;</td></tr>
<tr><td class="num" id="LN197">197</td><td class="line"> } <span class='keyword'>while</span> (startN != <span class='macro'>NULL<span class='expansion'>((void *)0)</span></span>);</td></tr>
<tr><td class="num" id="LN198">198</td><td class="line"> </td></tr>
<tr><td class="num" id="LN199">199</td><td class="line"> <span class='keyword'>return</span> <span class='macro'>OK<span class='expansion'>0</span></span>;</td></tr>
<tr><td class="num" id="LN200">200</td><td class="line">}</td></tr>
<tr><td class="num" id="LN201">201</td><td class="line"> </td></tr>
<tr><td class="num" id="LN202">202</td><td class="line"><span class='comment'>/**</span></td></tr>
<tr><td class="num" id="LN203">203</td><td class="line"> <span class='comment'>* Given a List, frees all memory associated with this list.</span></td></tr>
<tr><td class="num" id="LN204">204</td><td class="line"> <span class='comment'>*</span></td></tr>
<tr><td class="num" id="LN205">205</td><td class="line"> <span class='comment'>* @param list LinkedList * ..ptr to the list</span></td></tr>
<tr><td class="num" id="LN206">206</td><td class="line"> <span class='comment'>*/</span></td></tr>
<tr><td class="num" id="LN207">207</td><td class="line"><span class='keyword'>void</span> free_list(LinkedList *list) {</td></tr>
<tr><td class="num" id="LN208">208</td><td class="line"> printf(<span class='string_literal'>"----------------------\n"</span>);</td></tr>
<tr><td class="num" id="LN209">209</td><td class="line"> printf(<span class='string_literal'>"freeing list...\n"</span>);</td></tr>
<tr><td class="num" id="LN210">210</td><td class="line"> </td></tr>
<tr><td class="num" id="LN211">211</td><td class="line"> <span class='keyword'>if</span> (list != <span class='macro'>NULL<span class='expansion'>((void *)0)</span></span> &amp;&amp; <span class="mrange">list-&gt;size &gt; 0</span>) {</td></tr>
<tr><td class="num"></td><td class="line"><div id="Path2" class="msg msgEvent" style="margin-left:25ex"><table class="msgT"><tr><td valign="top"><div class="PathIndex PathIndexEvent">2</div></td><td><div class="PathNav"><a href="#Path1" title="Previous event (1)">&#x2190;</a></div></td></td><td>Assuming the condition is true</td><td><div class="PathNav"><a href="#Path3" title="Next event (3)">&#x2192;</a></div></td></tr></table></div></td></tr>
<tr><td class="num"></td><td class="line"><div id="Path3" class="msg msgControl" style="margin-left:5ex"><table class="msgT"><tr><td valign="top"><div class="PathIndex PathIndexControl">3</div></td><td><div class="PathNav"><a href="#Path2" title="Previous event (2)">&#x2190;</a></div></td></td><td>Taking true branch</td><td><div class="PathNav"><a href="#Path4" title="Next event (4)">&#x2192;</a></div></td></tr></table></div></td></tr>
<tr><td class="num" id="LN212">212</td><td class="line"> Node * startN = list-&gt;first;</td></tr>
<tr><td class="num" id="LN213">213</td><td class="line"> Node * temp = list-&gt;first;</td></tr>
<tr><td class="num" id="LN214">214</td><td class="line"> </td></tr>
<tr><td class="num" id="LN215">215</td><td class="line"> <span class='keyword'>do</span> {</td></tr>
<tr><td class="num" id="LN216">216</td><td class="line"> <span class="mrange">free(temp)</span>;</td></tr>
<tr><td class="num"></td><td class="line"><div id="Path4" class="msg msgEvent" style="margin-left:13ex"><table class="msgT"><tr><td valign="top"><div class="PathIndex PathIndexEvent">4</div></td><td><div class="PathNav"><a href="#Path3" title="Previous event (3)">&#x2190;</a></div></td></td><td>Memory is released</td><td><div class="PathNav"><a href="#EndPath" title="Next event (5)">&#x2192;</a></div></td></tr></table></div></td></tr>
<tr><td class="num" id="LN217">217</td><td class="line"> startN = <span class="mrange">startN-&gt;next</span>;</td></tr>
<tr><td class="num"></td><td class="line"><div id="EndPath" class="msg msgEvent" style="margin-left:22ex"><table class="msgT"><tr><td valign="top"><div class="PathIndex PathIndexEvent">5</div></td><td><div class="PathNav"><a href="#Path4" title="Previous event (4)">&#x2190;</a></div></td></td><td>Use of memory after it is freed</td></tr></table></div></td></tr>
<tr><td class="num" id="LN218">218</td><td class="line"> temp = startN;</td></tr>
<tr><td class="num" id="LN219">219</td><td class="line"> } <span class='keyword'>while</span> (startN != <span class='macro'>NULL<span class='expansion'>((void *)0)</span></span>);</td></tr>
<tr><td class="num" id="LN220">220</td><td class="line"> </td></tr>
<tr><td class="num" id="LN221">221</td><td class="line"> free(list);</td></tr>
<tr><td class="num" id="LN222">222</td><td class="line"> }</td></tr>
<tr><td class="num" id="LN223">223</td><td class="line">}</td></tr>
<tr><td class="num" id="LN224">224</td><td class="line"> </td></tr>
<tr><td class="num" id="LN225">225</td><td class="line"><span class='comment'>/**</span></td></tr>
<tr><td class="num" id="LN226">226</td><td class="line"> <span class='comment'>* Given a List and a key, iterates through the whole List and returns</span></td></tr>
<tr><td class="num" id="LN227">227</td><td class="line"> <span class='comment'>* the string of the first node which contains the key</span></td></tr>
<tr><td class="num" id="LN228">228</td><td class="line"> <span class='comment'>*</span></td></tr>
<tr><td class="num" id="LN229">229</td><td class="line"> <span class='comment'>* @param list LinkedList * ..ptr to the list</span></td></tr>
<tr><td class="num" id="LN230">230</td><td class="line"> <span class='comment'>* @param key int .. the key of the Node to get the String from</span></td></tr>
<tr><td class="num" id="LN231">231</td><td class="line"> <span class='comment'>*</span></td></tr>
<tr><td class="num" id="LN232">232</td><td class="line"> <span class='comment'>* @return OK | ERROR</span></td></tr>
<tr><td class="num" id="LN233">233</td><td class="line"> <span class='comment'>*/</span></td></tr>
<tr><td class="num" id="LN234">234</td><td class="line"><span class='keyword'>char</span> * get_string(LinkedList *list, <span class='keyword'>int</span> key) {</td></tr>
<tr><td class="num" id="LN235">235</td><td class="line"> printf(<span class='string_literal'>"----------------------\n"</span>);</td></tr>
<tr><td class="num" id="LN236">236</td><td class="line"> </td></tr>
<tr><td class="num" id="LN237">237</td><td class="line"> Node *startN = list-&gt;first; <span class='comment'>//get first</span></td></tr>
<tr><td class="num" id="LN238">238</td><td class="line"> </td></tr>
<tr><td class="num" id="LN239">239</td><td class="line"> <span class='comment'>/* if only one node.. */</span></td></tr>
<tr><td class="num" id="LN240">240</td><td class="line"> <span class='keyword'>if</span>(list-&gt;size == 1)</td></tr>
<tr><td class="num" id="LN241">241</td><td class="line"> <span class='keyword'>return</span> startN-&gt;string;</td></tr>
<tr><td class="num" id="LN242">242</td><td class="line"> </td></tr>
<tr><td class="num" id="LN243">243</td><td class="line"> <span class='comment'>/* iterate through list and find Node where node-&gt;key == key */</span></td></tr>
<tr><td class="num" id="LN244">244</td><td class="line"> <span class='keyword'>while</span> (startN-&gt;next != <span class='macro'>NULL<span class='expansion'>((void *)0)</span></span>) {</td></tr>
<tr><td class="num" id="LN245">245</td><td class="line"> <span class='keyword'>if</span> (startN-&gt;key == key)</td></tr>
<tr><td class="num" id="LN246">246</td><td class="line"> <span class='keyword'>return</span> startN-&gt;string;</td></tr>
<tr><td class="num" id="LN247">247</td><td class="line"> <span class='keyword'>else</span></td></tr>
<tr><td class="num" id="LN248">248</td><td class="line"> startN = startN-&gt;next;</td></tr>
<tr><td class="num" id="LN249">249</td><td class="line"> }</td></tr>
<tr><td class="num" id="LN250">250</td><td class="line"> </td></tr>
<tr><td class="num" id="LN251">251</td><td class="line"> <span class='keyword'>return</span> <span class='macro'>NULL<span class='expansion'>((void *)0)</span></span>;</td></tr>
<tr><td class="num" id="LN252">252</td><td class="line">}</td></tr>
<tr><td class="num" id="LN253">253</td><td class="line"> </td></tr>
<tr><td class="num" id="LN254">254</td><td class="line"><span class='comment'>/*************** MAIN **************/</span></td></tr>
<tr><td class="num" id="LN255">255</td><td class="line"><span class='keyword'>int</span> main(<span class='keyword'>void</span>) {</td></tr>
<tr><td class="num" id="LN256">256</td><td class="line"> </td></tr>
<tr><td class="num" id="LN257">257</td><td class="line"> LinkedList *list = init_list();</td></tr>
<tr><td class="num" id="LN258">258</td><td class="line"> </td></tr>
<tr><td class="num" id="LN259">259</td><td class="line"> insert_beginning(list, 1, <span class='string_literal'>"im the first"</span>);</td></tr>
<tr><td class="num" id="LN260">260</td><td class="line"> insert_end(list, 2, <span class='string_literal'>"im the second"</span>);</td></tr>
<tr><td class="num" id="LN261">261</td><td class="line"> insert_end(list, 3, <span class='string_literal'>"im the third"</span>);</td></tr>
<tr><td class="num" id="LN262">262</td><td class="line"> insert_end(list, 4, <span class='string_literal'>"forth here"</span>);</td></tr>
<tr><td class="num" id="LN263">263</td><td class="line"> </td></tr>
<tr><td class="num" id="LN264">264</td><td class="line"> print_list(list);</td></tr>
<tr><td class="num" id="LN265">265</td><td class="line"> remove_end(list);</td></tr>
<tr><td class="num" id="LN266">266</td><td class="line"> print_list(list);</td></tr>
<tr><td class="num" id="LN267">267</td><td class="line"> remove_beginning(list);</td></tr>
<tr><td class="num" id="LN268">268</td><td class="line"> print_list(list);</td></tr>
<tr><td class="num" id="LN269">269</td><td class="line"> remove_end(list);</td></tr>
<tr><td class="num" id="LN270">270</td><td class="line"> print_list(list);</td></tr>
<tr><td class="num" id="LN271">271</td><td class="line"> printf(<span class='string_literal'>"string at node with key %d = '%s' \n"</span>,2,get_string(list, 2));</td></tr>
<tr><td class="num" id="LN272">272</td><td class="line"> <span class="mrange">free_list(list)</span>;</td></tr>
<tr><td class="num"></td><td class="line"><div id="Path1" class="msg msgEvent" style="margin-left:5ex"><table class="msgT"><tr><td valign="top"><div class="PathIndex PathIndexEvent">1</div></td><td>Calling 'free_list'</td><td><div class="PathNav"><a href="#Path2" title="Next event (2)">&#x2192;</a></div></td></tr></table></div></td></tr>
<tr><td class="num" id="LN273">273</td><td class="line"> </td></tr>
<tr><td class="num" id="LN274">274</td><td class="line"> <span class='keyword'>return</span> <span class='macro'>OK<span class='expansion'>0</span></span>;</td></tr>
<tr><td class="num" id="LN275">275</td><td class="line">}</td></tr></table></body></html>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/********** GLOBALS *******************************/
#define OK 0
#define ERROR -1
/********** STRUCT AND TYPES DEFINTIONS ***********/
/* a node with key, data and reference to next node*/
typedef struct Node {
int key;
char string[1024];
struct Node *next; // pointer to next node
} Node;
/* the actual linked list: ref to first and last Node, size attribute */
typedef struct LinkedList {
struct Node *first;
struct Node *last;
int size;
} LinkedList;
/********** FUNCTION HEADERS **********************/
LinkedList* init_list();
void insert_end(LinkedList *list, int key, char string[]);
void insert_beginning(LinkedList *list, int key, char string[]);
int remove_end(LinkedList *list);
int remove_beginning(LinkedList *list);
int print_list(LinkedList *list);
void free_list(LinkedList *list);
char * get_string(LinkedList *list, int key);
/*********** FUNCTION DEFINITIONS ***************/
/**
* init_list Returns an appropriately (for an empty list) initialized struct List
*
* @return LinkedList * ..ptr to the newly initialized list
*/
LinkedList * init_list() {
printf("initializing list...\n");
LinkedList *list = (LinkedList*) malloc(sizeof(LinkedList));
list->first = NULL;
list->last = NULL;
list->size = 0;
return list;
}
/**
* Given a List, a key and a string adds a Node containing this
* information at the end of the list
*
* @param list LinkedList * ..ptr to LinkedList
* @param key int .. key of the Node to be inserted
* @param string char[] .. the string of the Node to be inserted
*/
void insert_end(LinkedList *list, int key, char string[]) {
printf("----------------------\n");
list->size++; // increment size of list
// intialize the new Node
Node* newN = (Node*) malloc(sizeof(Node));
newN->key = key;
strcpy(newN->string, string);
newN->next = NULL;
Node* oldLast = list->last; // get the old last
oldLast->next = newN; // make new Node the next Node for oldlast
list->last = newN; // set the new last in the list
printf("new Node(%p) at end: %d '%s' %p \n", newN, newN->key, newN->string,newN->next);
}
/**
* Given a List, a key and a string adds a Node, containing
* this information at the beginning of the list
*
* @param list LinkedList * ..ptr to LinkedList
* @param key int .. key of the Node to be inserted
* @param string char[] .. the string of the Node to be inserted
*/
void insert_beginning(LinkedList *list, int key, char string[]) {
printf("----------------------\n");
list->size++; // increment size of list
Node* oldFirst = list->first; //get the old first node
/* intialize the new Node */
Node* newN = (Node*) malloc(sizeof(Node));
newN->key = key;
strcpy(newN->string, string);
newN->next = oldFirst;
list->first = newN; // set the new first
/* special case: if list size == 1, then this new one is also the last one */
if (list->size == 1)
list->last = newN;
printf("new Node(%p) at beginning: %d '%s' %p \n", newN, newN->key,newN->string, newN->next);
}
/**
* Removes the first Node from the list
*
* @param list LinkedList * .. ptr to the List
*
* @return OK | ERROR
*/
int remove_beginning(LinkedList *list) {
printf("----------------------\n");
if (list->size <= 0)
return ERROR;
list->size--;
Node * oldFirst = list->first;
printf("delete Node(%p) at beginning: '%d' '%s' '%p' \n", oldFirst,oldFirst->key, oldFirst->string, oldFirst->next);
free(list->first); //free it
list->first = oldFirst->next;
oldFirst = NULL;
return OK;
}
/**
* Removes the last Node from the list.
*
* @param list LinkedList * .. ptr to the List
*
* @return OK | ERROR
*/
int remove_end(LinkedList *list) {
printf("----------------------\n");
/* special case #1 */
if (list->size <= 0)
return ERROR;
/* special case #2 */
if (list->size == 1) {
free(list->first);
list->first = NULL;
list->last = NULL;
return OK;
}
printf("delete Node(%p) at end: '%d' '%s' '%p' \n", list->last,list->last->key, list->last->string, list->last->next);
list->size--; // decrement list size
Node * startNode = list->first;
/* find the new last node (the one before the old last one); list->size >= 2 at this point!*/
Node * newLast = startNode;
while (newLast->next->next != NULL) {
newLast = newLast->next;
}
free(newLast->next); //free it
newLast->next = NULL; //set to NULL to denote new end of list
list->last = newLast; // set the new list->last
return OK;
}
/**
* Given a List prints all key/string pairs contained in the list to
* the screen
*
* @param list LinkedList * .. ptr to the List
*
* @return OK | ERROR
*/
int print_list(LinkedList *list) {
printf("----------------------\n");
if (list->size <= 0)
return ERROR;
printf("List.size = %d \n", list->size);
Node *startN = list->first; //get first
/* iterate through list and print contents */
do {
printf("Node#%d.string = '%s', .next = '%p' \n", startN->key,startN->string, startN->next);
startN = startN->next;
} while (startN != NULL);
return OK;
}
/**
* Given a List, frees all memory associated with this list.
*
* @param list LinkedList * ..ptr to the list
*/
void free_list(LinkedList *list) {
printf("----------------------\n");
printf("freeing list...\n");
if (list != NULL && list->size > 0) {
Node * startN = list->first;
Node * temp = list->first;
do {
free(temp);
startN = startN->next;
temp = startN;
} while (startN != NULL);
free(list);
}
}
/**
* Given a List and a key, iterates through the whole List and returns
* the string of the first node which contains the key
*
* @param list LinkedList * ..ptr to the list
* @param key int .. the key of the Node to get the String from
*
* @return OK | ERROR
*/
char * get_string(LinkedList *list, int key) {
printf("----------------------\n");
Node *startN = list->first; //get first
/* if only one node.. */
if(list->size == 1)
return startN->string;
/* iterate through list and find Node where node->key == key */
while (startN->next != NULL) {
if (startN->key == key)
return startN->string;
else
startN = startN->next;
}
return NULL;
}
/*************** MAIN **************/
int main(void) {
LinkedList *list = init_list();
insert_beginning(list, 1, "im the first");
insert_end(list, 2, "im the second");
insert_end(list, 3, "im the third");
insert_end(list, 4, "forth here");
print_list(list);
remove_end(list);
print_list(list);
remove_beginning(list);
print_list(list);
remove_end(list);
print_list(list);
printf("string at node with key %d = '%s' \n",2,get_string(list, 2));
free_list(list);
return OK;
}
$ clang -pedantic -Weverything list.c
list.c:18:16: warning: padding size of 'struct LinkedList' with 4 bytes to alignment boundary [-Wpadded]
typedef struct LinkedList {
^
list.c:41:14: warning: no previous prototype for function 'init_list' [-Wmissing-prototypes]
LinkedList * init_list() {
^
list.c:25:13: note: this declaration is not a prototype; add 'void' to make it a prototype for a zero-parameter function
LinkedList* init_list();
^
void
list.c:14:18: warning: padding struct 'struct Node' with 4 bytes to align 'next' [-Wpadded]
struct Node *next; // pointer to next node
^
list.c:76:50: warning: format specifies type 'void *' but the argument has type 'Node *' (aka 'struct Node *') [-Wformat-pedantic]
printf("new Node(%p) at end: %d '%s' %p \n", newN, newN->key, newN->string,newN->next);
~~ ^~~~
list.c:76:80: warning: format specifies type 'void *' but the argument has type 'struct Node *' [-Wformat-pedantic]
printf("new Node(%p) at end: %d '%s' %p \n", newN, newN->key, newN->string,newN->next);
~~ ^~~~~~~~~~
list.c:105:56: warning: format specifies type 'void *' but the argument has type 'Node *' (aka 'struct Node *') [-Wformat-pedantic]
printf("new Node(%p) at beginning: %d '%s' %p \n", newN, newN->key,newN->string, newN->next);
~~ ^~~~
list.c:105:86: warning: format specifies type 'void *' but the argument has type 'struct Node *' [-Wformat-pedantic]
printf("new Node(%p) at beginning: %d '%s' %p \n", newN, newN->key,newN->string, newN->next);
~~ ^~~~~~~~~~
list.c:125:63: warning: format specifies type 'void *' but the argument has type 'Node *' (aka 'struct Node *') [-Wformat-pedantic]
printf("delete Node(%p) at beginning: '%d' '%s' '%p' \n", oldFirst,oldFirst->key, oldFirst->string, oldFirst->next);
~~ ^~~~~~~~
list.c:125:105: warning: format specifies type 'void *' but the argument has type 'struct Node *' [-Wformat-pedantic]
printf("delete Node(%p) at beginning: '%d' '%s' '%p' \n", oldFirst,oldFirst->key, oldFirst->string, oldFirst->next);
~~ ^~~~~~~~~~~~~~
list.c:156:57: warning: format specifies type 'void *' but the argument has type 'struct Node *' [-Wformat-pedantic]
printf("delete Node(%p) at end: '%d' '%s' '%p' \n", list->last,list->last->key, list->last->string, list->last->next);
~~ ^~~~~~~~~~
list.c:156:105: warning: format specifies type 'void *' but the argument has type 'struct Node *' [-Wformat-pedantic]
printf("delete Node(%p) at end: '%d' '%s' '%p' \n", list->last,list->last->key, list->last->string, list->last->next);
~~ ^~~~~~~~~~~~~~~~
list.c:195:86: warning: format specifies type 'void *' but the argument has type 'struct Node *' [-Wformat-pedantic]
printf("Node#%d.string = '%s', .next = '%p' \n", startN->key,startN->string, startN->next);
~~ ^~~~~~~~~~~~
list.c:275:2: warning: no newline at end of file [-Wnewline-eof]
}
^
13 warnings generated.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment