Note that there are some explanatory texts on larger screens.

plurals
  1. POc++ use of *& as parameter (Pointer fun, tertiary tree method)
    text
    copied!<p>I'm trying to write a method which adds a key-value pair to a tertiary tree, but I'm obviously doing something wrong as I get a segfault whenever I reach the marked code</p> <pre><code>void Tree::add(int k, Node *&amp;r) { cout&lt;&lt;"add"&lt;&lt;endl; if(r==NULL){ r = new Node(k); //check(heap area); } </code></pre> <p><strong>begin problem code</strong></p> <pre><code> else if(r-&gt;keyCount == 1){ cout&lt;&lt;"adding second key"; if(r-&gt;getKey() &lt; k){ Node * temp = new Node(r-&gt;getKey(),k,r-&gt;data[0],0); delete r; r = temp; r-&gt;keyCount++; cout&lt;&lt;"test"&lt;&lt;endl; } else { Node * temp = new Node(k,r-&gt;getKey(),0,r-&gt;data[0]); delete r; r = temp; r-&gt;keyCount++; cout&lt;&lt;"test"&lt;&lt;endl; } </code></pre> <p><strong>end code</strong></p> <pre><code> } else if(k &lt; r-&gt;getKey()) { cout&lt;&lt;"left"&lt;&lt;endl; add(k,r-&gt;child[Node::L]); } else if(r-&gt;keyCount &gt; 1 &amp;&amp; k &lt; r-&gt;getKey(1)) { cout&lt;&lt;"middle"&lt;&lt;endl; add(k,r-&gt;child[Node::M]); } else if(r-&gt;keyCount &gt; 1 &amp;&amp; k &gt; r-&gt;getKey(1)) { cout&lt;&lt;"right"&lt;&lt;endl; add(k,r-&gt;child[Node::R]); } else r = new Node(k); } </code></pre> <p>What I'm trying to do is, in the case that there is only 1 out of the 2 keys used in this particular node, replace the current node with a new node which has the keys in the appropriate places (lesser val in key[0], greater val in key[1]) How do I do this properly?</p> <p>My code apparently deletes both the address AND pointer for the old node, but doesn't properly reassign the pointer to the new node.</p> <p><strong>EDIT</strong> updated code. the output is as follows:</p> <pre><code>% p4 Enter pairs consisting of an int and a double. I create a ternary tree, keeping the data in order, by int. Finish entering data by pressing ^d 2 2 add Entering the pair: 2, 2 1 1 add adding second key to current node test Entering the pair: 1, 1 -1 -1 add left add Entering the pair: -1, -1 3 3 add right Segmentation Fault </code></pre> <p><strong>EDIT 2</strong> Here's a link to a zip containing the entire project if you want to look at all the code: <a href="http://sdrv.ms/WSrLfv" rel="nofollow">http://sdrv.ms/WSrLfv</a></p> <p><strong>EDIT 3</strong> More error data - output from gdb on crash</p> <pre><code>Program received signal SIGSEGV, Segmentation fault. 0x08051628 in getData (x=@0x8047554) at testTree.cc:26 26 x[k]=d; Current language: auto; currently c++ </code></pre> <p><strong>EDIT 4</strong> stepping through gdb to the segfault:</p> <pre><code>Breakpoint 1, Tree::add (this=0x8047554, k=3, r=@0x8047554) at tree.cc:58 58 cout&lt;&lt;"add"&lt;&lt;endl; (gdb) n add 61 if(r==NULL){ (gdb) n 65 else if(r-&gt;keyCount == 1){ (gdb) n 87 else if(k &lt; r-&gt;getKey()) (gdb) n 92 else if(r-&gt;keyCount &gt; 1 &amp;&amp; k &lt; r-&gt;getKey(1)) (gdb) n 97 else if(r-&gt;keyCount &gt; 1 &amp;&amp; k &gt; r-&gt;getKey(1)) (gdb) n 99 cout&lt;&lt;"right"&lt;&lt;endl; (gdb) n right 100 add(k,r-&gt;child[Node::R]); (gdb) n Breakpoint 1, Tree::add (this=0x8047554, k=3, r=@0x806416c) at tree.cc:58 58 cout&lt;&lt;"add"&lt;&lt;endl; (gdb) n add 61 if(r==NULL){ (gdb) n 62 r = new Node(k); (gdb) n 107 } (gdb) n 107 } (gdb) n Tree::operator[] (this=0x8047554, index=3) at tree.cc:47 47 return *(locate(index,root)-&gt;data); (gdb) n 48 } (gdb) n Program received signal SIGSEGV, Segmentation fault. 0x08051628 in getData (x=@0x8047554) at testTree.cc:26 26 x[k]=d; (gdb) </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