title: How to implement resize() to change capacity of dynamic member data in c++
body: <p>I need to implement the function <strong>resize() :</strong> </p>
  
  <pre><code> void IntSet::resize(int new_capacity)
   {
       if (new_capacity &lt; used)
           new_capacity = used;
       if (used == 0)
           new_capacity = 1;
  
       capacity = new_capacity;
  
       int * newData = new int[capacity];
  
       for (int i = 0; i &lt; used; ++i)
           newData[i] = data[i];
  
       delete [] data;
       data = newData;
  }
  </code></pre>
  
  <p>inside the functions :</p>
  
  <pre><code> IntSet IntSet::unionWith(const IntSet&amp; otherIntSet) const
   {
         IntSet unionSet = otherIntSet;
  
         for (int i = 0; i &lt; used; i++)
         {
             if (unionSet.contains(data[i]))
                unionSet.add(data[i]);
         }
         return unionSet;
   }
  </code></pre>
  
  <p>and this one:  <em>( NOTE: that I have it already inside the add() function but I think it is incorrect)</em></p>
  
  <pre><code> bool IntSet::add(int anInt)
   {
       if (contains(anInt) == false)
       {
          if (used &gt;= capacity)
             resize(used++);
  
          data[used++] = anInt;
  
          return true;
       }
       return false;
   }
  </code></pre>
  
  <p>The program compiles correctly without errors, but it does give me an error of  <strong><em>Segmentation fault</em></strong></p>
  
  <p><strong>NOTE: The main thing is that I need help in learning how to use the resize function to resize the <em>capacity</em> of the the dynamic member data. Also, I know vectors would help in this case, but we are not allowed to use vectors yet</strong> </p>
  
  <p>Here is the <strong>Special Requirements</strong> from professor:</p>
  
  <pre><code>&gt;Special Requirement (You will lose points if you don't observe this.) &lt;br/&gt;
  
  &gt;When calling resize *(while implementing some of the member functions)* to 
  &gt;increase the capacity of the dynamic arrays, use the following resizing 
  &gt;rule (unless the new capacity has to be something else higher as dictated 
  &gt;by other &gt;overriding factors): &lt;br/&gt;
  &gt;
  &gt;*"new capacity" is "roughly 1.5 x old capacity" and at least "old capacity 
  &gt; + 1".* &lt;br/&gt;
  &gt;
  &gt;The latter *(at least " old capacity + 1 ")* is a simple way to take care 
  &gt;of the subtle case where " 1.5 x old capacity " evaluates (with truncation) 
  &gt;to the &gt;same as "old capacity". &lt;br/&gt;
  </code></pre>