title: Sort stack using recurssion
body: <p>So im trying to sort an array based stack but I keep getting the following issue: a.out(11849,0x115ce05c0) malloc: <strong>* error for object 0x7fc181402a80: pointer being freed was not allocated
  a.out(11849,0x115ce05c0) malloc: *</strong> set a breakpoint in malloc_error_break to debug.
  Here is my class specification</p>
  
  <pre><code>    StackType::StackType()
      {
          maxLength = 10;
          Top = -1;
          numArray = new int[maxLength];
      }
  
      //checks if stack is full
      bool StackType::isFull()
      {
          if(Top == maxLength -1)
              return true;
          else
              return false;
      }
  
      //checks if stack is empty
      bool StackType::isEmpty()
      {
          if(Top == -1)
              return true;
          else
              return false;
      }
  
      //insert numbers into stack
      void StackType::push(int num)
      {
          if(isFull())
              throw FullStack();
  
          Top++;
          numArray[Top] = num;
      }
  
      //deletes numbers in stack
      void StackType::pop()
      {
          if(isEmpty())
          throw EmptyStack();
          Top--;
      }
  
      //returns number at the top pf the stack
      int StackType::top()
      {
          if(isEmpty())
              throw EmptyStack();
          return numArray[Top];
      }
  
      //prints stack
      void StackType::printStack()
      {
          if(isEmpty())
              cout &lt;&lt; "Stack is empty\n";
          else
          {
              int tempIndex = 0;
              //top is the last position of array
              cout &lt;&lt; "Printing stack:\n";
              while(tempIndex &lt;= Top)
              {
                  cout &lt;&lt; numArray[tempIndex] &lt;&lt; endl;
                  tempIndex++;
              }
          }
      }
  
      //deletes array
      StackType::~StackType()
      {
          delete [] numArray;
      }
  </code></pre>
  
  <p>and here is my client code</p>
  
  <pre><code>#include "StackType.h"
  #include &lt;iostream&gt;
  using namespace std;
  StackType sortStack(StackType stack, StackType &amp;tempStack);
  int main ()
  {
  //read 10 ints into stack
  //output them first
  StackType currentStack, orderedStack;
  currentStack.push(21);
  currentStack.push(49);
  currentStack.push(7);
  currentStack.push(81);
  currentStack.push(5);
  currentStack.push(17);
  currentStack.push(2);
  currentStack.push(26);
  currentStack.push(42);
  currentStack.push(58);
  currentStack.printStack();
  cout &lt;&lt; "The following is the sorted stack\n";
  sortStack(currentStack, orderedStack);
  
  
  //implement recursion here
  
  //output stack again
  return 0;
  }
  StackType sortStack(StackType stack, StackType &amp;tempStack)
  {
  int current;
  if(stack.isEmpty() &amp;&amp; tempStack.isFull()) {
      cout &lt;&lt; "did it \n";
      return tempStack;
  }
  
  else
  {
      current = stack.top();
      stack.pop();
      if(tempStack.isEmpty())
      {
          cout &lt;&lt; "here1 \n";
          tempStack.push(current);
          return sortStack(stack, tempStack);
      }
      else
      {
          if(current &lt; tempStack.top())
          {
              stack.push(tempStack.top());
              tempStack.pop();
              tempStack.push(current);
              cout &lt;&lt; "here2 \n";
              return sortStack(stack, tempStack);
          }
          else
          {
              //when current is greater than temp.top
              tempStack.push(current);
              cout &lt;&lt; "here3 \n";
              return sortStack(stack, tempStack);
  
  
          }
      }
  }
  }
  </code></pre>