Note that there are some explanatory texts on larger screens.

plurals
  1. POLocal Variable has Value 0 Pass By Reference
    primarykey
    data
    text
    <p>My heap is based on my DynamicArray (ArrayList), and the DynamicArray adds/removes from the list by taking references of elements.</p> <p>My problem is with a function called fixIndexes. It is supposed to fix the indexes in the array when an element is added in the heap. The function works perfectly except for the last element is zero. I think it has to do with the local variable being popped off the stack after it is added to the array.</p> <p>I'm curious to know why this is the only variable that is 0, and maybe a better approach or solution.</p> <p>DynamicArray signatures:</p> <pre><code>bool add(T&amp; element); void set(uint index, T&amp; value); T&amp; remove(uint index); uint getSize(); </code></pre> <p>Code:</p> <pre><code>#include &lt;iostream&gt; #include "../DynamicArray/DynamicArray.h" using namespace std; using namespace triforce; DynamicArray&lt;int&gt; array; void fixIndexes(uint start, int&amp; elemToSet) { if(start &gt;= array.getSize()) { cout &lt;&lt; "Start: " &lt;&lt; start &lt;&lt; " elemToSet: " &lt;&lt; elemToSet &lt;&lt; "\n"; int temp = elemToSet; cout &lt;&lt; "Temp: " &lt;&lt; temp &lt;&lt; "\n"; array.add(temp); } else { cout &lt;&lt; "Start: " &lt;&lt; start &lt;&lt; " elemToSet: " &lt;&lt; elemToSet &lt;&lt; "\n"; int temp = array.get(start); array.set(start, elemToSet); fixIndexes((start * 2) + 1, temp); } } int main() { int val0 = 1; int val1 = 2; int val2 = 3; int val3 = 4; int val4 = 5; int val5 = 6; int val6 = 7; int val7 = 8; int val8 = 9; int val9 = 10; int val10 = 11; int val11 = 12; int val12 = 13; int val13 = 14; int val14 = 15; int val15 = 2; array.add(val0); array.add(val1); array.add(val2); array.add(val3); array.add(val4); array.add(val5); array.add(val6); array.add(val7); array.add(val8); array.add(val9); array.add(val10); array.add(val11); array.add(val12); array.add(val13); array.add(val14); cout &lt;&lt; array.toString() &lt;&lt; "\n"; fixIndexes(1, val15); cout &lt;&lt; array.toString() &lt;&lt; "\n"; return 0; } </code></pre> <p>output:</p> <pre><code>[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] Start: 1 elemToSet: 2 Start: 3 elemToSet: 2 Start: 7 elemToSet: 4 Start: 15 elemToSet: 8 Temp: 8 [1, 2, 3, 2, 5, 6, 7, 4, 9, 10, 11, 12, 13, 14, 15, 0] </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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. 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