Note that there are some explanatory texts on larger screens.

plurals
  1. PODynamic Array deleting old pointer and setting it to new array C++
    primarykey
    data
    text
    <p>I am trying to implement an insert() function that should insert a value into a boolean array and set the index equal to that value to 'true'. The <code>IntSet</code> object has a pointer, <code>value</code>, to an array of booleans and an int, <code>size</code>, to hold the size of the array. So <code>IntSet A(2, 4, 10);</code> will create an array of size 10 and the indexes at 2, 4, 10 will be set to true.</p> <p>The insert() function returns true or false depending on if it inserted the value or not, and it should resize the array if the inserted value is larger than the size of the array. So, <code>A.insert(1000);</code> would resize the array to size 1001 and set the value at index 1000 to true.</p> <p>My issue is with deleting the old array pointer and setting it to the new, resized array. No matter what I do, it always breaks at the delete[], and I'm not sure why.</p> <p>This is what I have so far:</p> <pre><code>bool IntSet::insert(int toInsert) { int tempSize = this-&gt;size; // if toInsert is greater than the number of elements in the array, call // copy constructor and assign new value to true if(toInsert &lt; this-&gt;size &amp;&amp; toInsert &gt;= 0) { value[toInsert] = true; return true; } IntSet largerSet(toInsert+1); if(toInsert &gt; this-&gt;size+1) { for(int i = 0; i &lt; largerSet.size+1; i++) { largerSet.value[i] = false; } largerSet.value[toInsert] = true; for(int i = 0; i &lt; tempSize+1; i++) { if(this-&gt;value[i] != false) { largerSet.value[i] = true; } } std::swap(value, largerSet.value); std::swap(size, largerSet.size); } return true; } </code></pre> <p>EDIT: used swap to move value to current array.</p> <p>I hope I was clear enough in my explanation, if you need more clarification I'm happy to provide more code. This is for a class assignment, so I'm not expecting a direct answer, but anything that can point me to the right direction will help immensely.</p> <p>Thanks all!</p>
    singulars
    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.
 

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