title: c++ Pointer Being Freed Was not Allocated
body: <p>Hello I'm trying to delete my dynamic memory within my class but I am getting the following error :</p>
  
  <pre><code>a.out(2830) malloc: *** error for object 0x7fb38a4039c0: pointer being freed was not allocated
  </code></pre>
  
  <p><em>*</em> set a breakpoint in malloc_error_break to debug
  Abort trap: 6</p>
  
  <p>I think is has something to do with my overloaded  "=" operator not accessing my class directly "&amp;".</p>
  
  <pre><code>class Polynomial{
      public:
          //Constructors / Destructors
          Polynomial();
          Polynomial(int tempNum, int * tempPoly);
          ~Polynomial();
  
          //Member Functions &amp; Operator overloading
          //Addition
          Polynomial operator+(Polynomial&amp; Poly);
          //Subtraction
          Polynomial operator-(Polynomial&amp; Poly);
          //Multiplication
          Polynomial operator*(Polynomial&amp; Poly);
          //Assignment
          Polynomial operator=(Polynomial Poly);
  
          //Insertion
          friend ostream&amp; operator&lt;&lt;(ostream&amp; os, Polynomial&amp; Poly);
          //Extraction
          friend istream&amp; operator&gt;&gt;(istream&amp; is, Polynomial&amp; Poly);
  
      private:
          //Data Members
          //Int array (degree of polynomial + 1)
          int * poly;
          int polyNum;
  };
  
  
  Implementation: 
  
  Polynomial::Polynomial(){
      polyNum = 0;
  }
  
  Polynomial::~Polynomial(){
      //Delete Dynamic Memory
      delete [] poly;
  }
  
  Polynomial::Polynomial(int tempNum, int *tempPoly){
  
      poly = new int[tempNum+1];
  
      polyNum = tempNum;
  
      for (int i=0; i &lt; tempNum; i++) {
          poly[i] = tempPoly[i];
      }
  }
  
  //Addition
  Polynomial Polynomial::operator+(Polynomial &amp;Poly){
      Polynomial temp;
  
      if(polyNum &gt; Poly.polyNum){
          temp.polyNum = polyNum;
      }
      else{
          temp.polyNum = Poly.polyNum;
      }
  
      temp.poly = new int[temp.polyNum + 1];
  
      for(int i=0; i &lt; temp.polyNum; i++){
          temp.poly[i] = Poly.poly[i] + poly[i];
      }
  
      return (temp);
  }
  
  //Subtraction
  Polynomial Polynomial::operator-(Polynomial&amp; Poly){
      Polynomial temp;
  
      if(polyNum &gt; Poly.polyNum){
          temp.polyNum = polyNum;
      }
      else{
          temp.polyNum = Poly.polyNum;
      }
  
      temp.poly = new int[temp.polyNum + 1];
  
      for(int i=0; i &lt; temp.polyNum; i++){
          temp.poly[i] = poly[i] - Poly.poly[i];
      }
  
      return (temp);
  }
  
  //Multiplication
  Polynomial Polynomial::operator*(Polynomial&amp; Poly){
  
      Polynomial temp;
  
      //make coefficient array
      temp.polyNum = (polyNum + Poly.polyNum) - 1;
  
      temp.poly = new int [temp.polyNum];
  
      for (int i = 0; i &lt; temp.polyNum; i++)
      {
          for (int j = 0; j &lt; Poly.polyNum; j++){
              temp.poly[i+j] += poly[i] * Poly.poly[j];
          }
      }
  
      return temp;
  }
  
  //Assignment
  Polynomial Polynomial::operator=(Polynomial Poly){
  
      polyNum = Poly.polyNum;
  
      poly = new int[polyNum+1];
  
      for (int i=0; i &lt; polyNum; i++) {
          poly[i] = Poly.poly[i];
      }
  
      return *this;
  }
  
  //Insertion
  ostream&amp; operator&lt;&lt;(ostream&amp; os, Polynomial&amp; Poly){
  
      for (int i=0; i &lt; Poly.polyNum; i++) {
          os &lt;&lt; Poly.poly[i] &lt;&lt; " x^" &lt;&lt; i;
  
          if(i != Poly.polyNum - 1){
              os &lt;&lt; " + ";
          }
      }
  
      return os;
  }
  
  //Extraction
  istream&amp; operator&gt;&gt;(istream&amp; is, Polynomial&amp; Poly){
  
      int numP = 0;
      int * tempP;
  
      is &gt;&gt; numP;
  
      tempP = new int [numP+1];
  
      for (int i=0; i &lt; numP; i++) {
          is &gt;&gt; tempP[i];
      }
  
      Poly.polyNum = numP;
  
      Poly.poly = new int[Poly.polyNum +1];
  
      for (int i=0; i &lt; Poly.polyNum; i++) {
          Poly.poly[i] = tempP[i];
      }
  
      delete [] tempP;
  
      return is;
  }
  </code></pre>
  
  <p>Main </p>
  
  <pre><code>int main(){
  
  // Input Polynomial #1 (P1)
  cout &lt;&lt; "Input polynominal p1: " &lt;&lt; endl;
  Polynomial P1;
  cin &gt;&gt; P1;
  
  // Output Polynominal
  cout &lt;&lt; "p1(x) = " &lt;&lt; P1 &lt;&lt; '\n' &lt;&lt; endl;
  
  // Input Polynomial #2 (P2)
  cout &lt;&lt; "Input polynominal p2: " &lt;&lt; endl;
  Polynomial P2;
  cin &gt;&gt; P2;
  
  // Output Polynominal
  cout &lt;&lt; "p2(x) = " &lt;&lt; P2 &lt;&lt; '\n' &lt;&lt; endl;
  
  // Copy P2 to P3 and output P3
  Polynomial P3;
  P3 = P2;
  cout &lt;&lt; "Copy p2 to p3, p3(x) = " &lt;&lt; P3 &lt;&lt; '\n' &lt;&lt; endl;
  
  // Add P1 to P2 and output to P3
  Polynomial P4;
  P4 = P1 + P2;
  cout &lt;&lt; "p3(x) = p1(x) + p2(x) = " &lt;&lt; P4 &lt;&lt; '\n' &lt;&lt; endl;
  
  // Subtract P1 from P2 and output to P3
  Polynomial P5;
  P5 = P1 - P2;
  cout &lt;&lt; "p3(x) = p1(x) - p2(x) = " &lt;&lt; P5 &lt;&lt; '\n' &lt;&lt; endl;
  
  // Subtract P2 from P1 and output to P3
  Polynomial P6;
  P6 = P2 - P1;
  cout &lt;&lt; "p3(x) = p2(x) - p1(x) = " &lt;&lt; P6 &lt;&lt; '\n' &lt;&lt; endl;
  
  // Multiply P1 by P2 and output to P3
  Polynomial P7;
  P7 = P1 * P2;
  cout &lt;&lt; "p3(x) = p1(x) * p2(x) = " &lt;&lt; P7 &lt;&lt; '\n' &lt;&lt; endl;
  
  return 0;
  </code></pre>
  
  <p>}</p>