title: How to fix segmentation fault for C++?
body: <p>I am getting a segmentation fault error when compiling my code. I don't know how to fix it. I am supposed to compile my Polynomial.cpp with my professor's poly_check.o, but I get a segmentation fault error. </p>
  
  <p>This is my Polynomial.h:</p>
  
  <pre><code>#ifndef POLYNOMIAL_H
  #define POLYNOMIAL_H
  
  #include &lt;iostream&gt;
  using namespace std;
  
  class Polynomial {
  
      private:
           double *coefficients;
           int size;
  
      public:
  
           Polynomial();
           Polynomial(double c[], int size);
           Polynomial(const Polynomial &amp;poly);
  
           ~Polynomial();
  
           void set(double c[], int size);
  
           inline int getDegree() const {
           return size - 1;
           }
  
           double f(double x)const;
  
           bool operator== (const Polynomial &amp;poly)const;
           Polynomial operator+ (const Polynomial &amp;poly)const;
  
           friend ostream &amp; operator &lt;&lt; (ostream &amp;out, const 
           Polynomial &amp;poly); 
           friend istream &amp; operator &gt;&gt; (istream &amp;in,  Polynomial 
           &amp;poly); 
  
   };
  
  #endif
  </code></pre>
  
  <p>This is my Polynomial.cpp:</p>
  
  <pre><code>#include "Polynomial.h"
  #include &lt;iostream&gt;
  #include &lt;cmath&gt;
  
  using namespace std;
  
  Polynomial::Polynomial() {
      size = 0;
      coefficients = NULL;
  }
  
  Polynomial::Polynomial(double c[], int size) {
  
      this-&gt;size = size;
      coefficients = new double[size];
  
      for (int i = 0; i &lt; size; i++) {
          coefficients[i] = c[i];
      }
  }
  
  Polynomial::Polynomial(const Polynomial &amp;poly) {
  
      if (coefficients != NULL)
          delete coefficients;
  
      this-&gt;size = poly.size;
      coefficients = new double[size];
  
      for (int i = 0; i &lt; size; i++)
          coefficients[i] = poly.coefficients[i];
  }
  
  Polynomial::~Polynomial() {
  
      if (coefficients != NULL)
          delete coefficients;
  } 
  
  void Polynomial::set(double c[], int size) {
  
      this-&gt;size = size;
      if (coefficients != NULL)
          delete coefficients;
  
      coefficients = new double[size];
  
      for (int i = 0; i &lt; size; i++)
          coefficients[i] = c[i];
  }
  
  double Polynomial::f(double x)const {
  
      double value = 0.0;
  
      for (int i = 0; i &lt; size; i++) {
          value += (coefficients[i] * pow(x, i));
      }
  
      return value;
  }
  
  bool Polynomial::operator== (const Polynomial &amp;poly)const {
  
      if (this-&gt;size != poly.size)
          return false;
  
      for (int i = 0; i &lt; size; i++) {
          if (poly.coefficients[i] != coefficients[i])
              return false;
      }
  
      return true;
  }
  
  
  Polynomial Polynomial::operator+ (const Polynomial &amp;poly)const {
  
      int maxSize = size;
      if (poly.size &gt; maxSize)
          maxSize = poly.size;
  
      double sum[maxSize] = {0.0};
  
      for (int i = 0; i &lt; size; i++) {
          sum[i] = coefficients[i];
      }
  
      for (int i = 0; i &lt; poly.size; i++) {
          sum[i] += poly.coefficients[i];
      }
  
      Polynomial sumP(sum, maxSize);
      return sumP;
  }
  
  ostream &amp;operator &lt;&lt; (ostream &amp;out, const Polynomial &amp;poly) {
  
      for (int i = poly.size - 1; i &gt;= 0; i--) {
  
          if (i != poly.size - 1) {
              if (poly.coefficients[i] &gt;= 0)
                  out &lt;&lt; " + ";
              else
                  out &lt;&lt; " - ";
          }
          out &lt;&lt; poly.coefficients[i];
          if (i == 0)
              continue;
          if (i == 1)
              out &lt;&lt; "x";
          else
              out &lt;&lt; "x^" &lt;&lt; i;   
      }
  
      return out;
  }
  
  istream &amp;operator &gt;&gt; (istream &amp;in,  Polynomial &amp;poly) {
  
      int degree;
      in &gt;&gt; degree;
  
      double c[100];
      int size = 0;
  
  
      while (in &gt;&gt; c[size]) {
          size++;
          if ((size-1) == degree)
              break;
      }
  
      poly.set(c, size);
      return in;
  }
  </code></pre>
  
  <p>This is my poly_test.cpp:</p>
  
  <pre><code>#include "Polynomial.h"
  #include &lt;iostream&gt;
  using namespace std;
  
  int main(void) {
  
      double c1[] = {0, 1, 2, 3, 4};
      double c2[] = {0, 1, 2, 3, 4, 5, 6};
  
      Polynomial p1(c1, 5);
      Polynomial p2(c2, 7);
  
      Polynomial p3;
      cout &lt;&lt; "Enter p3: ";
      cin &gt;&gt; p3;
  
      cout &lt;&lt; "p1: ";
      cout &lt;&lt; p1 &lt;&lt; endl;
  
      cout &lt;&lt; "p2: ";
      cout &lt;&lt; p2 &lt;&lt; endl;
  
      cout &lt;&lt; "p3: ";
      cout &lt;&lt; p3 &lt;&lt; endl;
  
      Polynomial p4;
      p4 = p1 + p2;
      cout &lt;&lt; "p4 = p1 + p2,  p4: ";
      cout &lt;&lt; p4 &lt;&lt; endl;
  
      double value = p1.f(2);
      cout &lt;&lt; "Evaluating p1 at x = 2, p1 = ";
      cout &lt;&lt; value &lt;&lt; endl;
  
      Polynomial p6(c1, 5);
      cout &lt;&lt; "p6: ";
      cout &lt;&lt; p6 &lt;&lt; endl;
      if (p6 == p1) {
         cout &lt;&lt; "p6 and p1 are equal. Equality test passed" &lt;&lt; 
     endl;
      }
       else {
          cout &lt;&lt; "Equality test failed" &lt;&lt; endl;
      }
  
      return 0;
  }
  </code></pre>
  
  <p>This is the error that I am getting:
  <a href="https://i.stack.imgur.com/Gg8rr.png" rel="nofollow noreferrer">segmentation fault error</a> </p>