title: Stacking C++ code working fine, but getting "glibc detected" at the end of running the code
body: <p>I'm currently working with "stacking" in my databases class and am having an issue with "<strong>* glibc detected *</strong>". From what I understand, it's an issue with either accessing memory that doesn't exist or memory leaking from some other source. I've gone through it several times and I can't see where any memory is being leaked, etc. </p>
  
  <p>Our assignment is to create a dynamic stacking program.</p>
  
  <p>My code consists of three files, but I'll compile them all into a single cpp:</p>
  
  <pre><code>//#include &lt;dstack.h&gt;
  #include &lt;iostream&gt;
  #include &lt;stdexcept&gt;
  
  using namespace std;
  
  class DStack
  {
      private:
      int *p;
      int *t;
      int length;
  
  public:
      DStack();
      ~DStack();
  
      void push(int);
      int pop();
      void display();
  };
  
  DStack::DStack()
  {
      length=0;
      cout&lt;&lt;"Stack has been created!"&lt;&lt;endl;
      p=new int[length];
  }
  
  DStack::~DStack()
  {
       delete [] p;
  }
  
  void DStack::push(int elem)
  {
      t = new int[length+1];
      for ( int i = 0; i &lt; length; i++ )
      {
          t[i] = p[i];
      }
      p = t;
      p[length] = elem;
      delete [] t;
      length++;
      cout&lt;&lt;length;
  
  }
  int DStack::pop()
  {
      t = new int[length-1];
      int ret = p[length-1];
      for ( int i = 0; i &lt; length-1; i++ )
      {
          t[i] = p[i];
      }
      p = t;
      delete [] t;
      length--;
      return ret;
  }
  
  void DStack::display()
  {
      cout&lt;&lt;length&lt;&lt;": ";
      for(int i = 0; i &lt; length; i++)
      cout&lt;&lt;p[i]&lt;&lt;" ";
      cout&lt;&lt;endl;
  }
  
  //main.cpp
  //#include &lt;dstack.h&gt;
  
  int main() 
  {
  cout&lt;&lt;"Enter a size for stack : ";
  DStack s1;
  
      s1.push(1);
      s1.display();
      s1.push(2);
      s1.push(3);
      s1.push(4);
      s1.push(5);
      s1.display();
      s1.pop();
      s1.display();
      s1.pop();
      s1.display();
      s1.pop();
      s1.display();
      s1.pop();
      s1.display();
      s1.pop();
      s1.display();
  
  
  }
  </code></pre>
  
  <p>Any help or direction would be greatly appreciated!
  The code seems to work fine as well, just that one error...</p>