title: Class Pointer Memory Issue
body: <p>I've trimmed this down so the problem still comes up.  I'll be able to get away with what I've created for my assignment, however, the memory management is causing some problems.  If I run this code as I've copied it, it gets hung up when it goes to the overloaded &lt;&lt; function.</p>
  
  <p>I believe that i've completed the memory allocation correctly, according to <a href="https://stackoverflow.com/questions/3466309/what-is-the-right-way-to-allocate-memory-in-the-c-constructor?rq=1">What is the right way to allocate memory in the C++ constructor?</a>.  I can't tell where the error is coming from.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;iomanip&gt;
  using namespace std;
  
  class Poly
  {
  private:
  int order; //order of the polynomial
  int size; //order + 1
  int * coeff;//pointer to array of coeff on the heap
  
  public:
      Poly();
      Poly(int Order);
      Poly(int Order, int * Coeff);
      ~Poly(){delete [] coeff; cout &lt;&lt; "Destructor\n";};
      Poly(const Poly &amp;rhs);
  
  //accessors &amp;  mutators
      void set(int * Coeff, int Order);
  
  //Overloaded Operators
      Poly operator+(const Poly &amp;rhs);
      Poly &amp; operator=(const Poly &amp;rhs);
      friend ostream &amp; operator&lt;&lt;(ostream &amp; Out, const Poly &amp;rhs);
  };
  
  int main()
  {
      int coeff1[ ] =  {-38,2,-24,6,4};
      int coeff2[ ] = {-38,2,-14,0,0,10,0,4};
      int size;
  
      bool flag;
  
      Poly P1(4, coeff1);
      Poly P2(7, coeff2);
      Poly P3;
      P3 = P1 + P2;
      cout &lt;&lt; "P1 + P2: " &lt;&lt; P3;
  
      return 0;
  }
  Poly::Poly()
  {
      order = 0;
      size = order + 1;
      coeff = new int[size];
      coeff[0] = 0;
      cout &lt;&lt; "Default Constructor\n";
  }
  Poly::Poly(int Order)
  {
      order = Order;
      size = order + 1;
      coeff = new int[size];
      for(int i(0); i &lt;= order; i++)
          coeff[i] = 0;
      cout &lt;&lt; "Order Constructor\n";
  }
  Poly::Poly(int Order, int * Coeff)
  {
      order = Order;
      size = order + 1;
      coeff = new int[size];
      for(int i(0); i &lt;= order; i++)
          coeff[i] = Coeff[i];
      cout &lt;&lt; "Complete Constructor\n";
  }
  Poly::Poly(const Poly &amp;rhs)
  {
      order = rhs.order;
      size = rhs.size;
      int *coeff = new int[size];
      for(int i(0); i &lt;= order; i++)
          coeff[i] = rhs.coeff[i];
      cout &lt;&lt; "Copy Constructor\n";
  }
  void Poly::set(int * Coeff, int Order)
  {
      order = Order;
      size = order + 1;
      for(int i(0); i &lt;= order; i++)
          coeff[i] = Coeff[i];
  }
  Poly Poly::operator+(const Poly &amp;rhs)
  {
      int neworder = max(order, rhs.order);
      int * newcoeff = new int[neworder+1];
      Poly temp(neworder, newcoeff);
      delete [] newcoeff;
  
      for(int i(0); i &lt;= temp.order; i++)
          temp.coeff[i] = 0;
      for(int i(0); i &lt;= order; i++)
          temp.coeff[i] = coeff[i];
      for(int i(0); i &lt;= rhs.order; i++)
          temp.coeff[i] += rhs.coeff[i];
  
      return Poly(temp.order, temp.coeff);
  }
  
  ostream &amp;operator &lt;&lt;(ostream&amp; out, const Poly &amp;source)
  {
      for(int i(source.order); i &gt;= 0; i--)
      {
          if(i == 1)
          {
              if(i == source.order)
                  if(source.coeff[i] == 1)
                      out &lt;&lt; "X";
                  else if(source.coeff[i] == -1)
                      out &lt;&lt; "-X";
                  else
                      out &lt;&lt; source.coeff[i] &lt;&lt; "X";
              else if(source.coeff[i] == 1)
                  out &lt;&lt; " + " &lt;&lt; "X";
              else if(source.coeff[i] == -1)
                  out &lt;&lt; " - " &lt;&lt; "X";
              else if(source.coeff[i] &gt; 0)
                  out &lt;&lt; " + " &lt;&lt; source.coeff[i] &lt;&lt; "X";
              else if(source.coeff[i] &lt; 0)
                  out &lt;&lt; " - " &lt;&lt; abs(source.coeff[i]) &lt;&lt; "X";
          }
  
          else if(i &gt; 1)
          {   
              if(i == source.order)
                  if(source.coeff[i] == 1)
                      out &lt;&lt; "X^" &lt;&lt; i;
                  else if(source.coeff[i] == -1)
                      out &lt;&lt; "-X^" &lt;&lt; i;
                  else
                      out &lt;&lt; source.coeff[i] &lt;&lt; "X^" &lt;&lt; i;
              else if(source.coeff[i] == 1)
                  out &lt;&lt; " + " &lt;&lt; "X^" &lt;&lt; i;
              else if(source.coeff[i] == -1)
                  out &lt;&lt; " - " &lt;&lt; "X^" &lt;&lt; i;
              else if(source.coeff[i] &gt; 1)
                  out &lt;&lt; " + " &lt;&lt; source.coeff[i] &lt;&lt; "X^" &lt;&lt; i;
              else if(source.coeff[i] &lt; -1)
                  out &lt;&lt; " - " &lt;&lt; abs(source.coeff[i]) &lt;&lt; "X^" &lt;&lt; i;
          }
          else
          {
              if(source.coeff[i] &gt; 0)
                  out &lt;&lt; " + " &lt;&lt; source.coeff[i];
              else if(source.coeff[i] &lt; 0)
                  out &lt;&lt; " - " &lt;&lt; abs(source.coeff[i]);
          }
      }   
      out &lt;&lt; endl;
  
     return out;
  } 
  Poly &amp; Poly::operator=(const Poly &amp;rhs)
  {
      order = rhs.order;  
      for(int i(0); i &lt;= rhs.order; i++)
          coeff[i] = rhs.coeff[i];
  
      return *this;
  }
  </code></pre>