title: malloc( ): memory corruption (fast) error C++
body: <p>I'm working in C++.  I'm working on a method to change the maximum capacity of a stack and I'm confused about an error I'm getting.  Below is my method.</p>
  
  <pre><code>void Stack::setCapacity(unsigned newCapacity){
  if(newCapacity&lt; this-&gt;getSize()){
      throw StackException("setCapacity()", 
      "the size is larger than the desired capacity");
  } else {
      if(newCapacity != myCapacity){
          Item * tempArray = new Item[newCapacity];
          if(newCapacity &lt; myCapacity){
              for(unsigned i=0; i&lt;newCapacity;i++){
                  tempArray[i] = myArray[i];
              }
          } else if (newCapacity &gt; myCapacity) {
              for(unsigned i=0; i&lt;myCapacity; i++){
                  tempArray[i] = myArray[i];
              }
          }
          for(unsigned i=0; i&lt;newCapacity; i++){
              myArray[i] = tempArray[i];
          }
          delete tempArray;
      }
      myCapacity = newCapacity;
  } 
  }
  </code></pre>
  
  <p>I have also written a test method to test that my setCapacity( ) method works. </p>
  
  <pre><code>void StackTester::setCapacityTest() {
  cout &lt;&lt; "- setCapacity... " &lt;&lt; flush;
  
  // empty stack
  Stack st7(5);
  assert(st7.getSize() == 0);
  assert(st7.getCapacity() == 5);
  st7.setCapacity(7);
  assert(st7.getCapacity() == 7);
  cout &lt;&lt; " 1 " &lt;&lt; flush;
  
  // partially filled stack - larger capacity
  Stack st8(5);
  assert(st8.getCapacity() == 5);
  st8.push(3);
  st8.push(4);
  st8.setCapacity(7);
  assert(st8.getCapacity() == 7);
  assert(st8.getTop() == 4);
  st8.pop();
  assert(st8.getTop() == 3);
  cout &lt;&lt; " 2 " &lt;&lt; flush;
  
  // size larger than new capacity
  try{
  Stack st9(3);
  st9.push(7);
  st9.push(4);
  st9.push(11);
  assert(st9.getSize() == 3);
  st9.setCapacity(2);
  cerr &lt;&lt; "setCapacity's new capacity is larger than the size";
  exit(1);
  } catch(StackException&amp; se){
      cout &lt;&lt; " 3 " &lt;&lt; flush;
  }
  
  // partially filled stack - smaller capacity
  Stack st10(5);
  assert(st10.getCapacity() == 5);
  st10.push(1);
  st10.setCapacity(2);
  assert(st10.getCapacity() == 2);
  assert(st10.getTop() == 1);
  cout &lt;&lt; " 4 " &lt;&lt; flush;
  
  // fully filled stack - larger capacity
  Stack st11(2);
  assert(st11.getCapacity() == 2);
  st11.push(3);
  st11.push(7);
  assert(st11.getTop() == 7);
  st11.setCapacity(3);
  assert(st11.getCapacity() == 3);
  cout &lt;&lt; " 5 " &lt;&lt; flush;
  
  cout &lt;&lt; " Passed!" &lt;&lt; endl;
  }
  </code></pre>
  
  <p>When I run each section of the test individually by commenting out the rest, everything works fine.  Each section of the test passes.  However, when I combine the sections and try to run the entire test, I get the following error:</p>
  
  <p><strong>* glibc detected ** * /home/.../StackProject: malloc(): memory corruption (fast): 0x0000000001e86030 *</strong></p>
  
  <p>Using the debugger, I've narrowed down the problem to the creation of myArray in the Stack.  For example, after successfully running " 1 " in my test, the creation of myArray in st8(5) in " 2 " causes the program to crash.</p>
  
  <p>My main source of confusion is due to the fact that each section passes individually but they do not pass collectively.  I'm not sure what to do about that.  Is my method written wrong?  If so, how should I correct it?  </p>
  
  <p>Thank you.</p>